Removing/Adding Money

Found out how, now i have a new problem…

Okay i got the money adding part right but i am trying to create a payday…

I have this in init.lua and it doesnt work… But it looks like it should

PAYDAY_TIME = 8
PAYDAY_AMOUNT = 50
PADYDAY_SOUND = true
function Timer( ply )
timer.Create( “payday_timer”, PAYDAY_INTERVAL, 0, function()
for k, ply in pairs(player.GetAll()) do
ply:Money_Add( PAYDAY_AMOUNT )
ply:PrintMessage( HUD_PRINTTALK, “SRP:Payday! $” … PAYDAY_AMOUNT)
if PAYDAY_SOUND then
ply:EmitSound(“items/ammo_pickup.wav”)
end
end
end )
end
hook.Add(“Seconds”,“time”,Timer)

To get you on the right track ->

Make it so when the player clicks the SPawnIcon it will run your command, so you can take his money.
To help you, here => RunConsoleCommand(“gm_spawn”, v) Change the gm_spawn to your console command. On the server check if it valid, and then remove a preset amount.

It says unknown command when i do it… I think its cause the include path is wrong… The npc menu is in the entity folders and the TakeMoney command is in the gamemode folder. How would i link those two?

[editline]07:29PM[/editline]

Does anyone have a idea of what i can do… I’ve been working on this for a couple hours now and i have gotten no where and its frustrating…

Bump

BUMP:

Okay i got the money adding part right but i am trying to create a payday…

I have this in init.lua and it doesnt work… But it looks like it should

Did you define / calculate PAYDAY_INTERVAL anywhere?

Furthemore, is “Seconds” a valid hook?

Yes its PAYDAY_TIME = 8

I changed it to time btw

and to be honest idk how to work hooks XD

Look at the gamemode hooks section of the wiki, because you need valid hooks, or else it won’t work; http://wiki.garrysmod.com/?title=Gamemode_Hooks

Which one should i use for this? it has to keep updating right? So would i use think?

Just use PlayerSpawn, because it is an indefinate timer, so you don’t need think.

Now its this located in init.lua

PAYDAY_TIME = 8
PAYDAY_AMOUNT = 50
PADYDAY_SOUND = true

Is that good?

[editline]12:13PM[/editline]

Works thank you!!! I really am glad that i was basically able to do this by my self, all i didnt know was the hook. And now i know about those :smiley: THANKS!

Looks pretty good. Btw: use [lua] [/ lua] tags around your code, it looks neater.

One more thing: Since you are now using the PlayerSpawn hook, the function will be called every single time a player spawns. You probably see what the problem is right now.
At the moment, your function cycles through all the players and creates a timer, while this should only be a one time thing.

You now have 2 choices. Either create a unique timer for each player on the PlayerInitialSpawn hook (this would give everybody their pay after the same interval, but at different times)
OR
you could create a global timer that cycles through the players every x seconds and gives them their pay at the same time. You would create this timer just after server initialization (sorry, I forgot the name and I don’t have access to the gmod wiki right now).

I personally would go for the global timer variant. Here is the code I would use:

[lua]
PAYDAY_TIME = 8
PAYDAY_AMOUNT = 50
PADYDAY_SOUND = true

function Timer()

timer.Create( "payday_timer", PAYDAY_TIME, 0, function()
    for k, ply in pairs(player.GetAll()) do
        ply:Money_Add( PAYDAY_AMOUNT )
        ply:PrintMessage( HUD_PRINTTALK, "SRP:Payday! $" .. PAYDAY_AMOUNT)
        if PAYDAY_SOUND then
        ply:EmitSound("items/ammo_pickup.wav")
        end
    end
end )

end
hook.Add(“SomeHook”,“Timer”,Timer) --look up the right hook, I forgot
[/lua]

[lua]
function Timer( ply )
timer.Create( “payday_timer”, PAYDAY_TIME, 0, function()
for k, ply in pairs(player.GetAll()) do
ply:AddMoney( PAYDAY_AMOUNT )
ply:PrintMessage( HUD_PRINTTALK, “SRP:Payday! $” … PAYDAY_AMOUNT)
if PAYDAY_SOUND then
ply:EmitSound(“items/ammo_pickup.wav”)
end
end
end )
end
hook.Add(“Initialize”,“Timer”,Timer)
[/lua]

Gameguysz do good yes? XD

you could just do this?
[lua]
timer.Create( “payday_timer”, PAYDAY_TIME, 0, function()
for k, ply in pairs(player.GetAll()) do
ply:AddMoney( PAYDAY_AMOUNT )
ply:PrintMessage( HUD_PRINTTALK, “SRP:Payday! $” … PAYDAY_AMOUNT)
if PAYDAY_SOUND then
ply:EmitSound(“items/ammo_pickup.wav”)
end
end
end )
[/lua]

And where would you put that?

In any serverside file.

I tend not to put code anywhere in a file if it’s not inside of a function. Also, not hooking it gives you pretty much no control over the time of execution (immediate) of the code.

You dont need a hook for it, theres no point wasting two lines of code for no reason