Help with Lua money system commands.

Hello! My friend Safixk, who I’ve been working on a new gamemode with, has recently posted a thread regarding this ( http://forum.facepunch.com/showthread.php?t=1314979 ). Through coding this gamemode, we’ve come across a problem we can’t seem to work around by ourselves.

We’re trying to add a command that allows us to set/add a player’s money.

It seems every time we fix one error, another (even harder to fix) error comes up.

How would we add a command that runs the AddMoney function, and adds the money to the player (this is for debugging the money system).

Code:
sh_player.lua


local meta = FindMetaTable("Player")
 
function meta:AddMoney(amount)
	local current_cash = self:GetMoney()
	self:SetMoney( current_cash + amount )
end
 
function meta:SetMoney(amount)
	self:SetNetworkedInt( "Money", amount )
	self:SaveMoney()
end
 
function meta:SaveMoney()
	local cash = self:GetMoney()
	self:SetPData("money", cash)
end
 
function meta:SaveMoneyTXT()
	file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString())
end
 
function meta:TakeMoney(amount)
   self:AddMoney(-amount)
end
 
function meta:GetMoney()
	return self:GetNetworkedInt( "Money" )
end

init.lua


MONEY_STARTAMOUNT = 20000
 
function FirstSpawn( ply )
	local cash = ply:GetPData("money")
 
	if cash == nil then
		ply:SetPData("money", MONEY_STARTAMOUNT)
		ply:SetMoney( MONEY_STARTAMOUNT )
	else
	ply:SetMoney( cash )
	end
	ply:Give( "weapon_physcannon" )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
 
function fPlayerDisconnect( ply )
	ply:SaveMoney()
	ply:SaveMoneyTXT()
end

Any thoughts regarding how to fix this?

Why do you have cash and current_cash?
Also, I am not very good a coding, but I can try to help…

Yes, I have them both. The code is very clear in itself.

We are actually wondering how we could create the command to add money to the player who runs the command through console, and that this command works the way it’s supposed to.

Also, couldn’t you just use a global cash instead of using locals?

They’re locals for a reason,
We are using two different numbers on two different operations to make the NetworkedINT sync and save properly with the right values. And current_cash, it’s there to take the current money the player has, perform arithmetic on it, and return the value so it can be saved and sync’d to the new Cash/Money value.

From what we can see, there is nothing wrong with the current code. We just want to know the proper way of making a command that runs the AddMoney function.

Halp D:

Don’t use the old tutorials from GM12, they are outdated and inefficient.

I know they are, but we couldn’t find anything else.
If you could point us to a proper way to do things, we’d really appreciate it.

Here’s an updated GM13 money module:
[lua]
local Player = FindMetaTable(“Player”)
if !Player then return end

function Player:GetMoney()
return self.Money or 0
end

function Player:CanAfford(num)
return (self:GetMoney()-num) >= 0
end

if SERVER then
util.AddNetworkString(“ply_Money”)
function Player:SetMoney(num)
self.Money = num
net.Start(“ply_Money”)
net.WriteEntity(self)
net.WriteInt(num, 32)
net.Broadcast()
end

function Player:AddMoney(num)
	self:SetMoney(self:GetMoney()+num)
end

else
net.Receive(“ply_Money”, function(len)
local ply = net.ReadEntity()
local num = net.ReadInt(32)
if !IsValid(ply) then return end
ply.Money = num
end)
end
[/lua]

I’m guessing this just goes on sh_player? and that there’s nothing changed on init?
If something breaks, I’ll just fix it myself.

Dear god, thank you so much. I’m going to write your name down, and buy you gold ASAP.

Thanks again.

No problem, and when you see “SERVER or CLIENT” in a file, it means it should be shared file.