• Simple Get() func returns nil clientside but not serverside
    11 replies, posted
Here is the code I am using [CODE]/*--------------------------------------------------------- Name: Money system Desc: ^ ---------------------------------------------------------*/ local playerMeta = FindMetaTable("Player"); if (SERVER) then util.AddNetworkString("ply_SetMoney"); function playerMeta:SetMoney(amount) net.Start("ply_SetMoney"); net.WriteInt(amount, 16); net.Send(self); self.i_Money = amount; end; function playerMeta:AddMoney(amount) self:SetMoney(self:GetMoney() + amount); end; function playerMeta:TakeMoney(amount) self:AddMoney(-amount); end; function playerMeta:SaveMoney() local _, folder = file.Find("money", "DATA"); if (!folder[1]) then file.CreateDir("money"); end; local uniqueID = self:UniqueID(); local amount = self:GetMoney(); file.Write("money/"..uniqueID..".txt", amount); end; function playerMeta:RestoreMoney() local uniqueID = self:UniqueID(); local content = file.Read("money/"..uniqueID..".txt", "DATA"); if (content) then local amount = tonumber(content); if (amount) then self:SetMoney(amount); end; end; end; else net.Receive("ply_SetMoney", function(length) local amount = net.ReadInt(16); LocalPlayer().i_Money = amount; end); end; function playerMeta:GetMoney() return self.i_Money; end;[/CODE] Error: [CODE][ERROR] gamemodes/glife/gamemode/visuals/hud/cl_hud.lua:230: attempt to concatenate a nil value 1. drawHUD - gamemodes/glife/gamemode/visuals/hud/cl_hud.lua:230 2. fn - gamemodes/glife/gamemode/visuals/hud/cl_hud.lua:125 3. unknown - addons/ulib-master/lua/ulib/shared/hook.lua:110[/CODE] Line 230 is this in cl_hud.lua [CODE]draw.SimpleText( "$" .. ply:GetMoney(), "font", x, y, Color( 255, 255, 255, 255 )[/CODE] If i print it in the players chat through the server, it returns the amount of money correctly. However when i try to GetMoney client side, it returns nil. Any ideas?
[code] function playerMeta:GetMoney() return self.i_Money or 0 end[/code]
[QUOTE=Robotboy655;49664534][code] function playerMeta:GetMoney() return self.i_Money or 0 end[/code][/QUOTE] If i add that, won't it just display 0 on the client?
Not if you set the value on client to a number.
[QUOTE=Robotboy655;49664546]Not if you set the value on client to a number.[/QUOTE] Im confused as to what you mean. Do you mean like set the var i_Money in a clientside file?
[QUOTE=Austin1346;49664541]If i add that, won't it just display 0 on the client?[/QUOTE] It pretty much just says unless we have a number for i_Money use the value 0 by default. When the client loads the UI element initially the server will not have sent a value to be stored in i_Money so it needs to use 0 to start off with. [code] return self.i_Money or 0 [/code] Lua interprets this as return the value of self.i_Money if it exists, if it does not exist (hasn't been set) then return 0 by default.
[QUOTE=demogod;49664569]It pretty much just says unless we have a number for i_Money use the value 0 by default. When the client loads the UI element initially the server will not have sent a value to be stored in i_Money so it needs to use 0 to start off with.[/QUOTE] Okay, i did that and it displays 0 for money on the HUD until i call either SetMoney(), AddMoney(), or TakeMoney(). Although this solves it, i don't want to give/set/take money from the player in order to fix it. How would i get around this?
[QUOTE=Austin1346;49664581]Okay, i did that and it displays 0 for money on the HUD until i call either SetMoney(), AddMoney(), or TakeMoney(). Although this solves it, i don't want to give/set/take money from the player in order to fix it. How would i get around this?[/QUOTE] So to network (send) the value to the player you're going to want to hook the [URL="https://wiki.garrysmod.com/page/GM/PlayerAuthed"]PlayerAuthed [/URL]hook for example. When a player joins the server you are going to want to find their information and send it to them as they load into the server. When that happens, the value will no longer be zero.
[QUOTE=demogod;49664599]So to network (send) the value to the player you're going to want to hook the [URL="https://wiki.garrysmod.com/page/GM/PlayerAuthed"]PlayerAuthed [/URL]hook for example. When a player joins the server you are going to want to find their information and send it to them as they load into the server. When that happens, the value will no longer be zero.[/QUOTE] Thank you very much, I forgot that PlayerAuthed even exists. This should work, right? (Placed in the same shared file as the money) [CODE]if SERVER then util.AddNetworkString( "sendMoneyToPlayer" ) function sendInfoToPlayer( ply, stmID, unID ) net.Start( "sendMoneyToPlayer" ) net.WriteInt( ply:GetMoney(), 16 ) net.Send( ply ) end hook.Add( "PlayerAuthed", "sendInfoToPlayer", sendInfoToPlayer ) end[/CODE]
[QUOTE=demogod;49664599]So to network (send) the value to the player you're going to want to hook the [URL="https://wiki.garrysmod.com/page/GM/PlayerAuthed"]PlayerAuthed [/URL]hook for example. When a player joins the server you are going to want to find their information and send it to them as they load into the server. When that happens, the value will no longer be zero.[/QUOTE] Wont it be better to use PlayerInitialSpawn?
[QUOTE=geferon;49665092]Wont it be better to use PlayerInitialSpawn?[/QUOTE] On the wiki for InitialSpawn [QUOTE]Please note that this hook is called before the player has fully loaded. For example, trying to use the Entity:GetModel function will return the default model ("player/default.mdl") (It calls when the player is still seeing the 'Sending Client Info' screen)[/QUOTE] I don't know if that makes a difference. Also, the dumb rating doesn't mean disagree
[QUOTE=Austin1346;49665246]On the wiki for InitialSpawn I don't know if that makes a difference. Also, the dumb rating doesn't mean disagree[/QUOTE] I did dumb beacouse youre putting a string instead of a function on the third argument of hook.Add. [editline]3rd February 2016[/editline] Also i dont know if it makes a difference as well, thats why ive asked
Sorry, you need to Log In to post a reply to this thread.