Using timers inside of a gamemode function

Hello,
I’m trying to insert a timer into a function that calls the playerloadout hook for the murder gamemode. This is to create a delay before the game will give you your class weapons upon spawn, instead of players just killing as soon as the round starts. Sort of like a grace period.

However, I haven’t seem to figure out hot to do it correctly, as simple as it seems. It would often throw out errors wherever I put the timer, and did one of a few things:
-Spawned everyone as a default gordon model, and did not allow further gameplay other than starting the gamemode
-Not show anyone in the scoreboard and refused to start the gamemode entirely
-Did nothing but spit out error saying “timer” or “simple” was not a function or whatnot

Issues aside, this is the original function:

[lua]function GM:PlayerLoadout(ply)

ply:Give("weapon_rp_hands")
-- ply:Give("weapon_fists")

if ply:GetMurderer() then
	ply:Give("weapon_mu_knife")
end

end
[/lua]

I have attempted to do this
[lua]function GM:PlayerLoadout(ply)

ply:Give("weapon_rp_hands")
-- ply:Give("weapon_fists")

timer(15, function()
    if ply:GetMurderer() then
	ply:Give("weapon_mu_knife")
end
end)

end
[/lua]

and

[lua]function GM:PlayerLoadout(ply)

timer.Simple(15, ply:Give("weapon_rp_hands")
-- ply:Give("weapon_fists")

if ply:GetMurderer() then
	ply:Give("weapon_mu_knife")
end )

end[/lua]

however, both are obviously incorrect, and I am still learning. What would be the best way to insert a timer to delay the spawning of weapons to the player? Also, if the round ends before the timer is up, will the timer still play and repeat with each round?

Thanks!

Before asking Facepunch, you should really get familiar with the Garry’s Mod wiki

It’ll help you out a lot, especially since you clearly have the right idea to go about this but are unfamiliar with the syntax

You’d do something like this

[lua]function GM:PlayerLoadout(ply)
timer.Simple(15, function()
if ply:GetMurderer() then
ply:Give(‘weapon_mu_knife’)
end // We don’t put a ) here because we don’t have a ( on our if statement
end) // The reason we put the ) here is to close our calling of timer.Simple
end[/lua]

Now, think of it like this. Everything start starts, should have an end, right?

[lua]{
// This is one example, used for tables
}[/lua]

[lua](
// This is another
)[/lua]

Same thing goes for if/elseif statements.

[lua]if 0 == 0 then
// If the first statement is true, run whatever is here
end // And here, we end the if statement[/lua]

You made your timer correctly, but the tabbing wasn’t great. Also, when modifying a game-mode function, make sure you’re not adding it when it already exists because the new one will overwrite the old. If you want to add behavior, use hook.Add…

Make sure hook.Call is in PlayerSpawn ( it calls PlayerLoadout ). Another thing you could do is use hook.Add( “RoundStart”, “UniqueName”, function( ) … end ); where … is you’d loop through players and give weapons on new round. Where RoundStart is, you’d use the Murder Game-mode round-started hook ( better than using a timer on spawn ).

Also if you’re programming on a dedicated server instance be aware that timers won’t run without a player or bot in the server.

It seems like he’s editing the core files Acecool.

Thanks everyone. Each post was helpful in getting to my final outcome.
I ended up using the timer to put everyone into and out of god mode in the same function (probably not the best place but it works).

Much appreciated!