MySQLoo Basic Money System

How should I go about making a simple money system with MySQL? I’ve been looking through darkrp scripts, but I can’t seem to figure out how it’s done. I have a table set up, and a script that saves the players SteamID and 1 “token” in their account on PlayerInitialSpawn, but I don’t understand how to full implement a money system. DarkRP’s looks to me, a very inexperienced lua coder, like it is using PData and saving the variables in the table. I am just starting to grasp meta tables, fyi.

Mysqloo is a module used to connect to a databse, you cannot use it to make a stand alone money system.

I would try doing this first in SQLite then trying mysql, its pretty much the same concept except SQLite is generally faster and should be used unless you are using the data outside of the server.

You’re telling me that MySQL isn’t some sort of magical money system module made just for garrysmod?! :gasp:
I must’ve missed the part of the sticky thread stating all OPs are clueless dumbasses. I’ll clarify a bit.

Money System




local STARTMONEY = 15 -- This is the amount of cash that players start with
local PAYDAYS = true -- Should there be paydays? 	true = yes, 	false = no
local PAYDAY_AMOUNT = 5 -- If there are paydays, what should the salary be?
local PAYDAY_INTERVAL = 8 -- If there are paydays, what should the payday interval be? (Seconds)
local PAYDAY_SOUND = true -- If there are paydays, should a sound play when there's a payday?  true = yes, 	false = no
local MONEY_HUD = true -- Should the money be displayed on the screen? true = yes, false = no

if SERVER then -- If this file is on a server do: (SERVER)
	local pm = FindMetaTable("Player") -- We state that entities under the meta table "player" is called pm, just to make it easier!
	
	function PlayerMoneySpawn( ply )
		ply:Money_Create()
	end
	hook.Add("PlayerInitialSpawn", "MoneySystemStart", PlayerMoneySpawn)
	
	function pm:Money_Create() -- DONT TOUCH! THIS FUNCTION SAVES THE SHIT
		print("Creating money account for: "..self:Nick())
		if self:GetPData( "cash" ) == nil then -- if there is no data under "cash", create some!
			self:SetPData( "cash", STARTMONEY ) -- if there is no "cash" data, give them 15 cash to begin with!
		end
	end

	function pm:Money_Set( cash ) -- player:Money_Set = SET THE MONEY OF A PLAYER
		self:SetPData( "cash", tonumber(cash) ) -- Saves the cash to the server
		self:SetNWInt( "cash", tonumber(cash) ) -- Sends the amount to the players' screens
	end

	function pm:Money_Add( cash ) -- player:Money_Add = ADD MONEY FOR A PLAYER
		if cash > 0 then -- If the cash to give is over 0...
			local current = tonumber(self:GetPData( "cash" )) -- Get the old amount of money
			self:Money_Set( current + cash ) -- Set the money to: The old amount of money + the money wished to be added
		end
	end

	function pm:Money_Has( cash ) -- player:Money_Add = GET THE MONEY FOR A PLAYER
		local current = tonumber(self:GetPData( "cash" )) -- Get the old amount of money
		if current >= tonumber(cash) then -- if the old amount si over the amount checked...
			return true -- say it's true!
		else
			return false -- if not, say it's false!
		end
	end
	
	function pm:Money_Get() -- player:Money_Add = GET THE MONEY FOR A PLAYER
		local current = tonumber(self:GetPData( "cash" )) -- Get the old amount of money
		return current
	end

	function pm:Money_Take( cash )  -- player:Money_Add = TAKE MONEY FOR A PLAYER
		if self:Money_Has( cash ) then -- if you got the amount of money...
			local current = tonumber(self:GetPData( "cash" )) -- Get the old amount of money
			self:Money_Set( current - cash ) -- Take the old amount of money and take away the amount given!
		end
	end
	
	if PAYDAYS then
		timer.Create( "payday_timer", PAYDAY_INTERVAL, 0, function()
			for k, ply in pairs(player.GetAll()) do
				ply:Money_Add( PAYDAY_AMOUNT )
				if PAYDAY_SOUND then
					ply:EmitSound("items/ammo_pickup.wav")
				end
			end
		end	)
	end
	
else -- IF THIS FILE IS ON A PLAYER PC DO... (CLIENT) 
	
	local function Money_Draw()
		/////////////////////////////////////////////////////////////////
		///////// FOR ADVANCED USERS: MAKE A HUD HERE ///////////////
		/////////////////////////////////////////////////////////////////
		
		if not MONEY_HUD then return end
		
		local mulleh = LocalPlayer():GetNWInt( "cash" ) -- The amount of money you got
		draw.DrawText("Money: "..mulleh, "Trebuchet18", 60, 60, Color(255,255,255,255))
	end
	hook.Add("HUDPaint", "MoneyPainting", Money_Draw)
end


MySQL database



require ("mysqloo")                // Include the modules
 
local DATABASE_HOST = "*****"   // database host (can be an ip)
local DATABASE_PORT = 3306                // port to the database, you probably wont need to change this unless you get told to
local DATABASE_NAME = "gmod"       // name of the database
local DATABASE_USERNAME = "*****"        // username which you use to access it
local DATABASE_PASSWORD = "*****"          // password of the username

function connectToDatabase()
	databaseObject = mysqloo.connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME)
	databaseObject.onConnected = function() print("Database linked!") end
	databaseObject.onConnectionFailed = function() print("Failed to connect to the database.") end
	databaseObject:connect()
end
 
connectToDatabase()
 
function checkQuery(query)
    local playerInfo = query:getData()
    if playerInfo[1] ~= nil then
	return true
    else
	return false
    end
end
 
function FirstJoinMysql( ply )
    local query1 = databaseObject:query("SELECT * FROM players WHERE ID = '" .. ply:SteamID() .. "'")
    query1.onSuccess = function(q)
        if not checkQuery(q) then
	    local query2 = databaseObject:query("INSERT INTO players(ID, tokens) VALUES ('" .. ply:SteamID() .. "', 1)")		// else create the bugger
	    query2.onSuccess = function(q)  print("Created you!") end
	    query2.onError = function(q,e) print("something went wrong") end
	    query2:start()	
        else
            print("You are already created!")
        end
    end
    query1.onError = function(q,e) print("something went wrong when checking") end
    query1:start()
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", FirstJoinMysql )


I would like the tokens from the Money system to be saved to the appropriate DB column. So something like this maybe?



databaseObject:query("REPLACE INTO players VALUES('" .. ply:SteamID() .. "',"..self.GetPData("cash").."))


Your looking for:


UPDATE players SET money = 'amt' WHERE steamid = 'your steam id'

If you want me to help you make this add me on steam - modegg and I’ll help you do this whole thing.