Simple Restore money command not working?

So i have this


	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);
				print("-----------------------------------------" .. amount)
			end;
		end;
	end;

It calls every PlayerInitialSpawn. Both prints i added print out correctly, and the SetMoney command works fine, and the file(.txt) the addon creates is also being written/edited by the server. Also, it would help to mention my save command works correctly and is called ever PlayerDisconnect.

I don’t know if it would help but here is where I call it.


function GM:PlayerInitialSpawn( ply )

	ply:RestoreMoney()
	print( "Restored $" .. ply:GetMoney() .. " for " .. ply:Nick() )

end

If it’s not clear, the problem is that both prints work correctly and display what is expected, but my player’s money in my hud isn’t updated.

Here is the func that returns money.


function playerMeta:GetMoney()
	return self.i_Money or 0;
end;

If you need more information, please ask. Thanks! Also, this code wasn’t written by me, but i believe it is old code.

Wait, so what does the SetMoney function ACTUALLY set? Is it that variable from GetMoney or some other thing?

I cant find setmoney on the darkrp variables but i got this:
Just use this to set the money:
http://wiki.darkrp.com/index.php/Functions/Player/Server/setDarkRPVar

Sorry for the bad formatting, writing from my phone.

It’s the var from the GetMoney function.

[editline]2nd February 2016[/editline]

This is not for darkrp.

does the money variable sync to the player?

Please show us the setmoney function.

https://www.facepunch.com/showthread.php?t=1193966&p=38679427&viewfull=1#post38679427

This is what I’m using. All I did was call the save money on disconnect and call restore money on initial spawn.

Make sure its a shared file.

It’s a shared file

Add some prints and see if everything is being sent and recieved correctly both server and client side.

I think i’ve thought of the problem. The money, when I use the AddMoney function, it reloads the previous amount and adds the amount i tell it to like it should. I feel like it’s working and restoring the money, but the HUD just doesn’t show it until it gets updated. I can try adding a console command to show money, or a chat command/button to show it when I get home. The HUD SHOULD be showing it though, because it’s updated every frame?

Shows us the hud.
And add clientside prints please.

Im not at my computer right now, on my laptop away from home. But I can tell you that the HUD is just draw.SimpleText with a default font the displays ply:GetMoney() if that helps.

So yeah, just add clientside prints to see if u recieve the net

Sounds to me like your hud is not properly refreshing. Do you have it in a paint hook that updates on tick?

Yes it’s under a paint hook, I have it so it displays health and that works fine

I’ve figured out the problem, however i do not know how to solve it.
Here it is


function playerMeta:GetMoney()
	return self.i_Money or 0; //<--- the "or 0" part is why it's being set to 0 on initial spawn.
end;

Is it skipping self.i_Money because i didn’t call SetMoney yet, so technically the var self.i_Money is empty?


/*---------------------------------------------------------
   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; //<---- here is where the self.i_Money is created, im guessing GetMoney is returning 0 is because the var self.i_Money is nil until i call this function once
	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);
				print("-----------------------------------------" .. 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 or 0;
end;

bump

http://hastebin.com/igilijiceq.lua
That is the file, it’s shared

Here is the error


[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

I call ply:RestoreMoney() on InitialSpawn, and ply:SaveMoney() on PlayerDisconnected.
I really don’t want to use NWInt’s here, so im trying as hard as possible to get help so I can get this to work.

EDIT:
Just tried this, and it prints the amount of money


function GM:ShowSpare1( ply )

	ply:PrintMessage( HUD_PRINTTALK, "You have $" .. ply:GetMoney() )

end

It seems it doesn’t want to send the var to the client, even though GetMoney func is both a server and client-side function…
Serverside print returns correctly, clientside returns nil.