gmod 13 money module.

So I was looking at the wiki for a money script, and I found the SQLite tutorial to make one. I followed that, but I then saw brandonj4’s post containing an updated module.

It looks like this :



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, bank)
		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



It works almost perfectly, but I can’t get it to save data persisently, like the other one did. Do I have to use SQLite for that? If so, how would I do it?

Try use networked stuff

I posted an updated one in another thread, found here.

This uses Garry’s built in SQLite function “SetPData()”.
[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:SetPData(“money”, 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

local startMoney = 100
hook.Add("PlayerInitialSpawn", "sh_money.PlayerInitialSpawn", function(ply)
	timer.Simple(1, function()	
		if !IsValid(ply) then return end
		ply:SetMoney(ply:GetPData("money", startMoney))
	end)
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]

Ah, perfect :slight_smile: You’ve saved the day once again brandon!

edit:How to read names

It’s Brandon, and no problem

Maybe I’m really not understanding this, but won’t it always grab the starting money as their money when they join?



	hook.Add("PlayerInitialSpawn", "sh_money.PlayerInitialSpawn", function(ply)
		timer.Simple(1, function()	
			if !IsValid(ply) then return end
			ply:SetMoney(ply:GetPData("money", startMoney))


How would I save their money to a database of some kind so that it is persistent?

edit: I am testing this on single player if that would make a difference. But all I have to do to reset my money is leave the server and boom it sets back to 100 regardless of what I had it at before I left.

Edit: Nevermind I’m dumb. I didn’t copy the whole code. My fault.

I would suggest saving the data to either through text or SQLite. Whatever you do don’t even think about MySQL. I am suggesting these because you are only working with small amounts of data.