Adding number to a variable on spawn

Hey well i cant figure out whats wrong with my lua script it should work as the - works

any ideas i think it might have something to do with GM:PlayerSpawn? is it a gamemode function?

Money=800 --Money Start Value
Msg ("Initializing Buy Menu
")
function BuyMenu () – BuyMenu Function

end

function DesertEagle ( ply ) --Desert Eagle Buy Fuction
if Money >= 800 then
ply:Give( “weapon_deagle” ) – A DesertEagle
Money=- 800 – TakesAway 800 from Money “Works”
else
Msg ("You Do Not Have Enough Money!
")
end
end

function GM:PlayerSpawn(ply) --OnSpawn?
Money= + 800 --Should add 800 on Money “Doesn’t” Ingame i get a error “autorun/Guns.lua:17: unexpected symbol near ‘+’” without the = it says it expects a = near it
end

concommand.Add (“BuyMenu_DesertEagle”, DesertEagle)

I see there’s a little bit of misunderstanding here, see that’s not exactly how lua works. :slight_smile:
I suggest you read the documentation found here : http://wiki.garrysmod.com/?title=Lua

As for your specific problems you would do it this way :
[lua]
Money = 800
print(Money) – 800
Money = Money - 800
print(Money) – 0
Money = Money + 800
print(Money) – 800 again[/lua]

Also for a money system I strongly suggest you use a variable that is specific to each player, such as ply.Money.

thanks,kinda just getting into lua
so would i be able to use ply.Money = 800 to set the variable to 800 by default?

That sets ‘Money’ to -800.

Don’t += and all its friends work in Lua :open_mouth:

I tought not.:confused:

It would be nice tough. If possible it would make a worthy addition to UtilX.

uh i see so thats why i thought it worked as when i called the command a second time it was having -800 instead of 0 thanks
and i was getting the error because i was trying to set the variable to (+)800 which you cannot do but -800 you can do

No.
+=, -=, /=, *=, %=, ++ and – are all missing from normal Lua and Garry didn’t bother to add them.
It’s very annoying.

You can’t add that to UtilX, but you could be really weird and use things like % and ^ as += and -=, but that would make it impossible to understand for the reader.

[editline]11:36AM[/editline]

And not to mention it would mess up any code that used % and ^ :v:

Any idea how i could get this to work?

Money=800 --Money Start Value

function GiveMoney (amount)
–if ply:IsAdmin() then
Money = Money + (amount)
–else
–Msg ("You Need To Be Admin!
")
–end
end

concommand.Add (“BuyMenu_GiveMoney”, GiveMoney) --How would i be able to make it find (amount) in the console command so it would work something like BuyMenu_GiveMoney 800


Money=800 --Money Start Value

function GiveMoney (ply,cmd,args)
            local amount = args[1] --Args is a table, take the first value
            if !amount or !amount > 0 then return end -- If it's under 0 or there is no amount then end here
            if ply:IsAdmin() then
		        ply.Money = ply.Money + (amount)
	    else
		        ply:ChatPrint("You Need To Be Admin!")
	    end
end

concommand.Add ("BuyMenu_GiveMoney", GiveMoney) 

And please use


 or [lua] tags.

– Is comments for lua, so that may be why garry didn’t bother to add it.

Turning – into a math operator would break almost every code written. He should add all the other ones but leave out – and substitute something else for the – operation.

Having started in C++ and gone to lua, I really miss those little guys D:

Crazy Quebec that dosent seem to work for me i get this in console

This Is what i mean

] BuyMenu_Amount
You Have $800
] BuyMenu_GiveMoney
] BuyMenu_Amount
You Have $800
] BuyMenu_GiveMoney 100
autorun/Money.lua:5: attempt to compare number with boolean
] BuyMenu_Amount
You Have $800
] BuyMenu_GiveMoney “100”
autorun/Money.lua:5: attempt to compare number with boolean
] BuyMenu_Amount
You Have $800


-----------------------Code--------------------------

Money=800 --Money Start Value

function GiveMoney (ply,cmd,args)
local amount = args[1] --Args is a table, take the first value
if !amount or !amount > 0 then return end – If it’s under 0 or there is no amount then end here–shouldnt it have a < if its ment to end if its lessthan/under 0
if ply:IsAdmin() then
ply.Money = ply.Money + (amount)
else
ply:ChatPrint(“You Need To Be Admin!”)
end
end

function Amount (ply)
ply:ChatPrint(“You Have $”…Money…"
")
end
concommand.Add (“BuyMenu_GiveMoney”, GiveMoney)
concommand.Add (“BuyMenu_Amount”, Amount)

-----------------------Code--------------------------