How would i make the money variable unique per player ? i tryed something like ply.Money = 800 but that diddnt work any ideas ?
[lua]
Money = 800 -- Give it a starting value
local function givemoney(ply,cmd,args)
local number = tonumber(args[1])
if number and (number >= 0) and ply:IsAdmin() then
Money = Money + (number)
else
ply:ChatPrint("You Need To Be Admin!")
end
end
local function takemoney(ply,cmd,args)
local number = tonumber(args[1])
if number and (number >= 0) and ply:IsAdmin() then
Money = Money - (number)
else
ply:ChatPrint("You Need To Be Admin!")
end
end
local function amount (ply)
ply:ChatPrint("You Have $"..Money.."\n")
end
concommand.Add("BuyMenu_GiveMoney", givemoney)
concommand.Add("BuyMenu_TakeMoney", takemoney)
concommand.Add("BuyMenu_Amount", amount)
[/lua]
[QUOTE=Sartek;18164372]How would i make the money variable unique per player ? i tryed something like ply.Money = 800 but that diddnt work any ideas ?[/QUOTE]
1> use [lua] or [code] tags.
2> the player is only made in the script once it calls a functions wich gives the player as an argument.
3> heres your code. shuold work however untested since im at school
[lua]
--Bromvlieg is god!
--ply.Cash is the player's money. this script wil set it to 100 if it dont exists yet to prefent errors ^^
local function givemoney(ply,cmd,args)
local number = tonumber(args[1])
if number and ply:IsAdmin() then
if not ply.Cash then --if the player dint had any cash yet, lets give him 100!
ply.Cash = 100
end
ply.Cash = ply.Cash + number
else
ply:ChatPrint("You Need To Be Admin!")
end
end
local function takemoney(ply,cmd,args)
local number = tonumber(args[1])
if number and ply:IsAdmin() then
if not ply.Cash then --if the player dint had any cash yet, lets give him 100!
ply.Cash = 100
end
ply.Cash = ply.Cash - number
else
ply:ChatPrint("You Need To Be Admin!")
end
end
local function amount (ply,cmd,args)
ply:ChatPrint("You Have $".. (ply.Cash or 0).."\n") -- the or is to prefent erroring: atempt to call a nil value, so, if cash exists it wil do that, elses it wil turn in 0 ^^
end
concommand.Add("BuyMenu_GiveMoney", givemoney)
concommand.Add("BuyMenu_TakeMoney", takemoney)
concommand.Add("BuyMenu_Amount", amount)
[/lua]
[lua]
function SetInitialCash (ply)
ply.Cash = 100 --or whatever you want him to have at first
end
hook.Add("PlayerInitialSpawn", "SetInitialCash", SetInitialCash)
[/lua]
I'd put that in the script as well (below bromvileg's code) so that ply.Cash will always exist. I'm at college so I can't check the name of the hook, it's something like PlayerInitialSpawn anyway.
Also you don't have to go to the trouble of adding an if to check if ply.Cash exists, you can do:
[lua]ply.Cash = ply.Cash or 100[/lua]
Meaning if ply.Cash exists it'll keep its value or if it doesn't exist it'll get an initial value of 100. Not that using an if like you did is at all wrong.
If you store the player's money like that, the second they disconnect it will become nil.
[QUOTE=Salads;18168848]If you store the player's money like that, the second they disconnect it will become nil.[/QUOTE]
Yeah. You can save it if you want by using SetPData: [url]http://wiki.garrysmod.com/?title=Player.SetPData[/url]
It stores values in the SQL database sv.db so you can retrieve it later.
[QUOTE=yakahughes;18168884]Yeah. You can save it if you want by using SetPData: [url]http://wiki.garrysmod.com/?title=Player.SetPData[/url]
It stores values in the SQL database sv.db so you can retrieve it later.[/QUOTE]
I used that at first but I've found it does way too many queries uselessly. Using sql commands directly is almost just as simple (with the aid of the wiki) and much more efficient.
Interesting, I've never come across PData before.
[QUOTE=Crazy Quebec;18169759]I used that at first but I've found it does way too many queries uselessly. Using sql commands directly is almost just as simple (with the aid of the wiki) and much more efficient.[/QUOTE]
Can you give like an example or something? I've never used it before so it'd be good to know.
Example script which loads and saves a players money
[lua]
--How much money do new players get?
local defaultMoney = 100
--This function ensures that the database even has a table for the data
function InitMoney()
--If the table does not exist in the database...
if (!sql.TableExists( "player_money" )) then
--Create the table
--I use double quotes here because table and column names usually are in single quotes
--When creating a table. TEXT means the column contains any amount of text.
--NOT NULL means that an error will occur if you do not fill in that column when
--Adding a new row. If this error occurs then the row will fail to be added. It ensures
--That we fill in all the data. INT(10) means the column has an Integer that can be 10
--numbers long. Integers do not contain decimal places
sql.Query( "CREATE TABLE 'player_money'('steamid' TEXT NOT NULL, 'money' INT(10) NOT NULL)" )
end
end
--Hook it. This will run when the server is initializing
hook.Add( "Initialize", "InitMoneySystem", InitMoney )
--This function grabs a players money when they first join, or adds them to the database
--and gives them the new player money if they are joining for the first time
function InitialSpawnMoney(ply)
local steamid = ply:SteamID()
--"SELECT *" means get all columns of data from the row
--A row is an entry of data and a column is the different values we are storing
--In this case the columns are the players steamid and money.
--SQLStr automatically makes a string safe to use in sql and adds double quotes around it
--Hence why im using single quotes for this query. Making a string safe includes
--escaping certain characters so they dont cause an error
local result = sql.Query( 'SELECT * FROM player_money WHERE steamid='..SQLStr( steamid ) )
--If they are not in the database, add them!
if (#result == 0) then
sql.Query( 'INSERT INTO player_money VALUES('..SQLStr( steamid )..', '..defaultMoney..')' )
ply.Money = defaultMoney
else
--The format of the result set is result[row#][column_name] where row# is a number from
--1 to the number of rows found and column_name is the name of the column
ply.Money = result[1]['money']
end
end
--Hook it. This will run when the player has finished joining
hook.Add( "PlayerInitialSpawn", "PlayerJoinedMoneySystem", InitialSpawnMoney )
--This function saves the players money when they leave
function DisconnectSaveMoney(ply)
local steamid = ply:SteamID()
--Update the value in the database
sql.Query( 'UPDATE player_money SET money='..ply.Money..' WHERE steamid='..SQLStr( steamid ) )
end
--Hook it. This will run when the player disconnects.
--Their player object is still valid until after this hook finishes
hook.Add( "PlayerDisconnected", "DisconnectSaveMoneySystem", DisconnectSaveMoney )
[/lua]
P.S. That is probably the most commenting i have ever done in a script, no matter what language i was programming xD
Where have I been? I thought the database and the SQL implementation were part of some module a guy made, and they're in the game by default?
Thanks ill have a look at it tomorrow as its pretty late here
[QUOTE=MegaJohnny;18180970]Where have I been? I thought the database and the SQL implementation were part of some module a guy made, and they're in the game by default?[/QUOTE]
You can access sv.db and cl.db which are local files on your server with built in stuff. To access a database on another server, you need a module, like tmysql or andyvincent's module.
You may also look into just saving their databases as txt files. I find this to be more organized just incase I need to go in and change something by hand. With SQLite, there is also a chance for corruption, which has happened to me more than once on my servers that used to use SQLite. Trust me, it's a lot worse to lose an entire database then maybe one person's txt file :v:
[QUOTE=yakahughes;18184303]You can access sv.db and cl.db which are local files on your server with built in stuff. To access a database on another server, you need a module, like tmysql or andyvincent's module.[/QUOTE]
Makes sense.
When the builtin commands are used, I presume which database it's stored on depends on which of the client or server it's running on?
[quote=Gmod Wiki]In the "garrysmod" main folder, there is "sv.db" for the server database, and "cl.db" for the client database.[/quote]
[url]http://wiki.garrysmod.com/?title=Sql[/url]
[QUOTE=MegaJohnny;18187873]Makes sense.
When the builtin commands are used, I presume which database it's stored on depends on which of the client or server it's running on?[/QUOTE]
Sort: Yes.
Gmt 2001 your script has a error when i run it in game
[code]
Hook 'PlayerJoinedMoneySystem' Failed: autorun/MoneySave.lua:34: attempt to get length of local 'result' (a nil value)
[/code]
[QUOTE=Sartek;18198992]Gmt 2001 your script has a error when i run it in game
[code]
Hook 'PlayerJoinedMoneySystem' Failed: autorun/MoneySave.lua:34: attempt to get length of local 'result' (a nil value)
[code]
Can anyone link me to a how to post on facepunch correctly?
or how to use [code][/QUOTE]
[lua] [ /lua]
And try this for lines #31 - 34:
[lua]
local result = sql.QueryValue( 'SELECT money FROM player_money WHERE steamid='..SQLStr( steamid ) )
--If they are not in the database, add them!
if ( !result ) then
[/lua]
Thanks and ill try that now
Edit:Nah that diddn't work i got
[code]Hook 'PlayerJoinedMoneySystem' Failed: autorun/MoneySave.lua:34: attempt to get length of local 'result' (a nil value)
[/code]
Fuck dont think i restarted gmod
[QUOTE=Sartek;18199102]Thanks and ill try that now[/QUOTE]
Actually, before you do that, it's not going to work. Money will be a string when it should be a number, use this:
[lua]
local result = tonumber( sql.QueryValue( 'SELECT money FROM player_money WHERE steamid='..SQLStr( steamid ) ) )
[/lua]
Not getting a error now
[code]
--How much money do new players get?
local defaultMoney = 100
--This function ensures that the database even has a table for the data
function InitMoney()
--If the table does not exist in the database...
if (!sql.TableExists( "player_money" )) then
--Create the table
--I use double quotes here because table and column names usually are in single quotes
--When creating a table. TEXT means the column contains any amount of text.
--NOT NULL means that an error will occur if you do not fill in that column when
--Adding a new row. If this error occurs then the row will fail to be added. It ensures
--That we fill in all the data. INT(10) means the column has an Integer that can be 10
--numbers long. Integers do not contain decimal places
sql.Query( "CREATE TABLE 'player_money'('steamid' TEXT NOT NULL, 'money' INT(10) NOT NULL)" )
end
end
--Hook it. This will run when the server is initializing
hook.Add( "Initialize", "InitMoneySystem", InitMoney )
--This function grabs a players money when they first join, or adds them to the database
--and gives them the new player money if they are joining for the first time
function InitialSpawnMoney(ply)
local steamid = ply:SteamID()
--"SELECT *" means get all columns of data from the row
--A row is an entry of data and a column is the different values we are storing
--In this case the columns are the players steamid and money.
--SQLStr automatically makes a string safe to use in sql and adds double quotes around it
--Hence why im using single quotes for this query. Making a string safe includes
--escaping certain characters so they dont cause an error
local result = tonumber( sql.QueryValue( 'SELECT money FROM player_money WHERE steamid='..SQLStr( steamid ) ) )
--If they are not in the database, add them!
if ( !result ) then
sql.Query( 'INSERT INTO player_money VALUES('..SQLStr( steamid )..', '..defaultMoney..')' )
ply.Money = defaultMoney
else
--The format of the result set is result[row#][column_name] where row# is a number from
--1 to the number of rows found and column_name is the name of the column
ply.Money = result[1]['money']
end
end
--Hook it. This will run when the player has finished joining
hook.Add( "PlayerInitialSpawn", "PlayerJoinedMoneySystem", InitialSpawnMoney )
--This function saves the players money when they leave
function DisconnectSaveMoney(ply)
local steamid = ply:SteamID()
--Update the value in the database
sql.Query( 'UPDATE player_money SET money='..ply.Money..' WHERE steamid='..SQLStr( steamid ) )
end
--Hook it. This will run when the player disconnects.
--Their player object is still valid until after this hook finishes
hook.Add( "PlayerDisconnected", "DisconnectSaveMoneySystem", DisconnectSaveMoney )
[/code]
[editline]08:49PM[/editline]
Cant get any of them to work with my buy code
they work perfectly other than that
autorun/Moneynosave.lua:42: attempt to compare number with nil
i know im not calling the variable right
[lua]
--Bromvlieg is god!
--ply.Cash is the player's money. this script wil set it to 100 if it dont exists yet to prevent errors ^^
local function givemoney(ply,cmd,args)
local number = tonumber(args[1])
if number and ply:IsAdmin() then
if not ply.Cash then --if the player dint had any cash yet, lets give him 100!
ply.Cash = 100
end
ply.Cash = ply.Cash + number
else
ply:ChatPrint("You Need To Be Admin!")
end
end
local function takemoney(ply,cmd,args)
local number = tonumber(args[1])
if number and ply:IsAdmin() then
if not ply.Cash then --if the player dint had any cash yet, lets give him 100!
ply.Cash = 100
end
ply.Cash = ply.Cash - number
else
ply:ChatPrint("You Need To Be Admin!")
end
end
local function amount (ply,cmd,args)
ply:ChatPrint("You Have $".. (ply.Cash or 0).."\n") -- the or is to prevent erroring: attempt to call a nil value, so, if cash exists it wil do that, elses it wil turn in 0 ^^
end
function SetInitialCash (ply)
ply.Cash = 100 --or whatever you want him to have at first
end
hook.Add("PlayerInitialSpawn", "SetInitialCash", SetInitialCash)
----------------------------------------------------------------
-- Buy Command's --
----------------------------------------------------------------
--This Function gives you a DesertEagle
function DesertEagle (ply)
if ply.Cash >= 800 then
ply:Give( "weapon_deagle" ) -- A DesertEagle
ply.Cash = ply.Cash - 800 -- TakesAway 800 from ply.Money
else
Msg ("You Do Not Have Enough Money! \n")
end
end
----------------------------------------------------------------
-- Buy Command's --
----------------------------------------------------------------
concommand.Add("BuyMenu_GiveMoney", givemoney)
concommand.Add("BuyMenu_TakeMoney", takemoney)
concommand.Add("BuyMenu_Amount", amount)
----------------------------------------------------------------
-- Buy Command's --
----------------------------------------------------------------
--Desert Eagle Concommand
concommand.Add ("BuyMenu_DesertEagle", DesertEagle)
----------------------------------------------------------------
-- Buy Command's --
----------------------------------------------------------------
[/lua]
You used my function to give the player an initial value of money but you didn't change the ply.Cash to ply.Money. So when you try to buy a DEagle ply.Money is still nil.
Line 42 is where you check if the player can afford the gun. If you give yourself some money with the other commands and then try to buy one it should work.
The bottom one is the one im trying to get to work there all ply.cash
doesent ply.cash mean 2 different things depending how you use it? as in ply.cash = 800 is a global variable
but using ply.cash another way makes it a player only variable?
Oh yeah, you've made it into ply.Cash. In that case you forgot to change ply.Money >= 800 to ply.Cash >= 800.
I have no idea what you're talking about with the global variable thing. If you type ply.Cash = 50 it'll assume you have an object called ply and you want to set a variable called Cash inside ply to 50.
Yeah i see that now yeah and i ment something like that
[editline]10:42PM[/editline]
Yeah it works should be able to finish off the rest of my game mode now
gooood, and dont rmeove my awsome credit line! (:D
Wow, get out. This board is about helping people with Lua problems, not seeing how much credit you can get from it.
[sp] Rated you a box [/sp]
[QUOTE=Sartek;18198992]Gmt 2001 your script has a error when i run it in game
[code]
Hook 'PlayerJoinedMoneySystem' Failed: autorun/MoneySave.lua:34: attempt to get length of local 'result' (a nil value)
[/code][/QUOTE]
my apologies. LUA is a bitch xD
Don't worry gmt2001 if yours worked i was gonna make another rp...
[QUOTE=Gbps;18201481]Wow, get out. This board is about helping people with Lua problems, not seeing how much credit you can get from it.
[sp] Rated you a box [/sp][/QUOTE]
relax && sarcasm
Sorry, you need to Log In to post a reply to this thread.