Hey guys, I made a code
function AddMoney(amount)
self:SetMoney( current_cash + amount )
end
concommand.Add("get_cash",AddMoney)
And it says: Invalid Command, but everything is right… Whats wrong?
Hey guys, I made a code
function AddMoney(amount)
self:SetMoney( current_cash + amount )
end
concommand.Add("get_cash",AddMoney)
And it says: Invalid Command, but everything is right… Whats wrong?
[lua]
function GiveMoney(ply,cmd,amount)
ply:SetMoney( current_cash + tonumber(amount[1]) )
end
concommand.Add(“Add_cash”,AddMoney)[/lua]
Fixed
Add_cash 100
Yes I am!
Where would I need to put this?
cl_init, init, shared or sh_player.lua?
Same file with the money functions.
You mean the Meta File (sh_player.lua)
local meta = FindMetaTable("Player") --Get the meta table of 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:GiveMoney(amount)
local current_cash = self:GetMoney()
self:SetMoney( current_cash - amount )
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)
--Add money function here
self:AddMoney(-amount)
end
function meta:GetMoney()
return self:GetNetworkedInt( "Money" )
end
Or init.lua
MONEY_STARTAMOUNT = 0 --Can be changed to your starting amount
function FirstSpawn( ply )
local cash = ply:GetPData("money") --Get the saved money amount
if cash == nil then --If it doesn't exist supply the player with the starting money amount
ply:SetPData("money", MONEY_STARTAMOUNT) --Save it
ply:SetMoney( MONEY_STARTAMOUNT ) --Set it to the networked ints that can be called from the client too
else
ply:SetMoney( cash ) --If not, set the networked ints to what we last saved
end
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
function PrintCash( pl )
pl:ChatPrint("Your cash is: " .. pl:GetMoney())
end
function fPlayerDisconnect( ply )
print("Player Disconnect: Money saved to SQLLite and TXT")
ply:SaveMoney()
ply:SaveMoneyTXT()
end
concommand.Add("cash",PrintCash)
function GM:DoPlayerDeath( ply, attacker, dmginfo )
ply:CreateRagdoll()
ply:AddDeaths( 1 )
if ( attacker:IsValid() && attacker:IsPlayer() ) then
if ( attacker == ply ) then
attacker:AddMoney( 100 )
else
attacker:AddMoney( 100 )
end
end
end
Put it in whatever server side file you want. It’s your code. You decide what’s more convenient for you.
I tried it, but it just gives an Invalid Command:
http://img409.imageshack.us/img409/2592/naamloosff.png
Please notice that this is a fretta gamemode, is this even possible?
[lua]function GiveMoney(ply,cmd,amount)
ply:SetMoney( ply:GetMoney() + tonumber(amount[1]) )
end
concommand.Add(“Add_cash”,AddMoney)[/lua]
Btw you might want to change the money to Money in your pl:SetNWInts() in sh_player and init.lua