LUA:Money System Part 2

From GMod Wiki

Jump to: navigation, search
Lua: Money System Part 2
Page white text.png Description:The seccond part in the money system tutorial.
link=User:KillerLUA Original Author:KillerLUA
Calendar.png Created:March 12, 2010

Contents


Carrying on

Well, we've setup the files. Added the functions, now all we need to do is setup the gamemode starting functions.

Now, let's setup the player spawn functions first. Go to your init.lua file of your gamemode

 
MONEY_STARTAMOUNT = 1000 --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 )
 

We're adding a function that checks if the cash is exists, if it doesn't set the players money to whatever the starting amount is.

Now, we're setting the cash on the players first spawn.

We've added most things, we just need a save on disconnect function.

In your init.lua add the following lines:

 
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_get",PrintCash)
 

And we're done. Here's all the code put together from all of the parts of this tutorial!

All of the code

init.lua:

 
MONEY_STARTAMOUNT = 1000 --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_get",PrintCash)
 

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: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
 
 

Notes and what you can extend

Notes

Extend Ideas

Yarin Kaul Icon ArrowSquare32 left.png Back to Part 1

Yarin Kaul Icon ArrowSquare32.png Back to KillerLUA Tutorials


Like this tutorial?

Visit here for weekly tutorials, if you like or/and this money system, please send a PM to me at facepunch on how your using it as all feedback is appreciated. Not only that, my facepunch PM inbox is empty anyway.

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox