Hi Facepunchers!
How do I get the ammount of money the users has, from the server, to the client?
I have the function getMoney(ply) in init.lua:
[lua]
function getMoney( ply )
if(file.Exists( "GDom\content\data\\" .. ply:UniqueID() .. ".txt" )) then
return tonumber(file.Read( "GDom\content\data\\" .. ply:UniqueID() .. ".txt"))
else
ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n")
file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", 15 )
ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$")
return 15
end
end
[/lua]
But I want to get the money into a variable, so I can draw it on the clients HUD. How do I do this?
[lua]
moneyText = "" -- money ammount
[/lua]
[highlight](User was banned for this post ("Wrong section" - mahalis))[/highlight]
Supposedly this is for in-game currencies, right?
Correct.
[editline]12:36PM[/editline]
The money system works, I just wanna draw the ammount of money to the clients HUD, so they dont get confused.
[url]http://wiki.garrysmod.com/?title=User_Messages[/url]
or on server (when money changes or gets set up) ply:ConCommand('setmoney '..tostring(getMoney(ply))) or something and make a command on client ( [url]http://wiki.garrysmod.com/?title=ConVar[/url] ) which receives this command and sets a local client's variable.
So many ways, I don't know which one would be efficient for your case.
Testing..
[editline]01:12PM[/editline]
Well, in the console it says, that it updated my money, but it doesnt update the HUD, whats wrong? (I get no errors)
cl_init.lua
[lua]
----------------------------
-- HUD
----------------------------
moneyText = 0
function updateMoney(uMoney)
moneyText = uMoney:ReadLong()
end
function drawHUD()
teamBoxHeight = 47
teamBoxWidth = 240
teamBoxX = ScrW() / 2 - teamBoxWidth / 2
teamBoxY = teamBoxHeight / 2 - 5
teamBoxBorder = 6
moneyBoxHeight = 47
moneyBoxWidth = 40
moneyBoxX = teamBoxX + 6 + teamBoxWidth
moneyBoxY = teamBoxY
moneyBoxBorder = 6
teamText = team.GetName( LocalPlayer():Team() )
draw.RoundedBox( teamBoxBorder, teamBoxX, teamBoxY, teamBoxWidth, teamBoxHeight, Color( 0, 0, 0, 150 ) )
draw.RoundedBox( moneyBoxBorder, moneyBoxX, moneyBoxY, moneyBoxWidth, moneyBoxHeight, Color( 0, 0, 0, 150 ) )
draw.SimpleText( teamText, "ScoreboardText", teamBoxX + teamBoxWidth / 2 - string.len(teamText) + 3, teamBoxY + teamBoxHeight / 2, team.GetColor( LocalPlayer():Team() ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText( tostring(moneyText) .. "$", "ScoreboardText", moneyBoxX + moneyBoxWidth / 2 - string.len(moneyText) + 3, moneyBoxY + moneyBoxHeight / 2, Color( 0, 255, 0, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
hook.Add("HUDPaint", "GDomHUD", drawHUD)
[/lua]
init.lua
[lua]
function saveMoney( ply, ammount )
if(file.Exists( "GDom\content\data\\" .. ply:UniqueID() .. ".txt" )) then
file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", ammount )
else
ply:PrintMessage(HUD_PRINTTALK, "The system didnt find your money file..\nCreating..\n")
file.Write( "GDom\content\data\\" .. ply:UniqueID() .. ".txt", 15 )
ply:PrintMessage(HUD_PRINTTALK, "Created! You do as default have 15$")
end
updateClient(ply, ammount)
end
function updateClient(ply, ammount)
local daPlayer = RecipientFilter()
daPlayer:AddPlayer(ply)
umsg.Start("updateMoney", daPlayer)
umsg.Long( ammount )
umsg.End()
print("Updated " .. ply:Name() .. "s money")
end
[/lua]
Hook the function updateMoney(uMoney) :)
[LUA]
usermessage.Hook("updateMoney",function(uMoney)
moneyText = uMoney:ReadLong()
end)
[/LUA]
Lol :lol: Good idea! XD.
[editline]01:18PM[/editline]
Worked perfectly! :yay:
[editline]01:18PM[/editline]
Smiley fail, lol.
Sorry, you need to Log In to post a reply to this thread.