Timer Problem

I have one problem with a timer I made.
There are no errors to begin with just to make it clear.
So here is the code:
[lua]function AutoTeamSelection( ply )

if ply:Team() == TEAM_UNASSIGNED then
timer.Create( ply:SteamID().."Auto", 9, 1, ply:SetTeam( math.random( 1, 2 ) ) )

else

timer.Destroy( ply:SteamID().."Auto" )
return

end

end

hook.Add( “PlayerSpawn”, “AutoTeamSelect”, AutoTeamSelection )[/lua]
Now of course, bear in mind that this snippet of code is experimental.
The problem is, the timer dosen’t seem to start but executes the function right away as if I had never put the ply:SetTeam() in the timer.

I might be doing something wrong, may someone help me out?
Thank you.

First of all, that’s going to do it every time they die and respawn. I see you have a check there, but you may want to hook into PlayerInitialSpawn instead.

If the timer is not created, it doesn’t need to be destroyed.

What it does in my gamemode is when the round is over we are set to TEAM_UNASSIGNED.
So if you stay unassigned for 9 seconds, you are set to a random team. But if we join a team before the delay then we delete the timer to stop it from choosing for us. Get it?

Ok.

You need to make a ‘callback’ function (not sure if I’m using the word right, whatever) to set the team, since it thinks that the function you want to call with the timer is the result of ply:SetTeam( math.random( 1, 2 ) ). The fourth argument is a function object, not a command, so you are actually calling ply:SetTeam( math.random( 1, 2 ) ) and setting the fourth parameter to timer.Create to the result of that call. So do this instead:

timer.Create( ply:SteamID()…“Auto”, 9, 1, function() ply:SetTeam( math.random( 1, 2 ) ) end )

Also you don’t need the ‘return’ statement since there is nothing after it.

He is correct about you needing to send a callback, however creating a new anonymous function is a waste of memory since your function already exists and timer.Create already accepts variable parameters.
[lua]timer.Create( ply:SteamID()…“Auto”, 9, 1, ply.SetTeam, ply, math.random( 1, 2 ) )[/lua]
It logically will be equivalent with the exception that the team is selected now and only set to it later. However both answers are equally random.

Ok, I think I get it now, thx for your help, I’ll go on and try it.

[editline]17:19[/editline] Now I don’t know if anyone will come in to help me out since it now makes 9 hours but I have another question.
Can it be possilbe to make the player spawn at the same time we set him to another team, in the same timer?

Thank you

Have you tried using:
[lua]timer.Create( ply:SteamID()…“Auto”, 9, 1, function() ply:Spawn() end )[/lua]

Or something similar to it?

Yes I did but it dosen’t work, anybody else got a clur or idea?

I do not understand that sentence, please rephrase it.

Based on that description here’s what I came up with :
[lua]function AssignAllPlayers()
for key,ply in ipairs(team.GetPlayers(TEAM_UNASSIGNED)) do
– Place the code to assign ply to a random team here
end
end

timer.Simple(9,AssignAllPlayers) – Place this line where you want to start the timer.[/lua]

This way 9 seconds after your round is over any player left in the TEAM_UNASSIGNED team will be assigned to a random team (You still have to put that bit in though).
Why do complicated when you can do simple? :smile:

Oh and also :
:eng101:

Haha, thank you Crazy Quebec, this seems fine, thx again.