Help with rounds

Hey. First of all I am really new with lua and I do my best to search through internet before posting help threads, but I have been stuck for 2 days. I am trying to code an FFA system that starts a round when atleast 2 players are online, ends the round when 1 player is alive, and stops starting new rounds if only 1 player is online.

I haven’t tried coding it all yet, I just have problems trying to make checks for how many players are online.

[lua]
local num = team.NumPlayers( 2 ) --theres only 1 team (besides spectator team) since its FFA so I assume this works
local canstart = false

function checkforplayers()
if num > 1 and canstart == false then
canstart = true
print(“Enough players have joined! Starting the round…”)
–more shit to trigger the round started
elseif num <= 1 and canstart == true then
canstart = false
print (“Waiting for players”)
–more shit to cause rounds not start
end
end
hook.Add( “PlayerConnect”, “playerscheck1”, checkforplayers );
hook.Add( “PlayerDisconnected”, “playerscheck2”, checkforplayers );
[/lua]
I’m not sure but maybe I didn’t understand the meaning of hooks properly coz I kinda feel something is wrong with the hook.Add’s.
Thank you in advance.

PS, I’d like to learn, not copypaste :slight_smile:

EDIT: Oh yeah and the problem itself is that when I check the canstart value through command I created, it isn’t updating itself when player connets/disconnects

for a start put this:
[lua]local num = team.NumPlayers( 2 )[/lua] inside your checkforplayers function. that way it will be updated every time its called.

[lua]
local canstart = false

function checkforplayers()
local num = team.NumPlayers( 2 )
if num > 1 and canstart == false then
canstart = true
print(“Enough players have joined! Starting the round…”)
–more shit to trigger the round started
elseif num <= 1 and canstart == true then
canstart = false
print (“Waiting for players”)
–more shit to cause rounds not start
end
end
hook.Add( “PlayerConnect”, “playerscheck1”, checkforplayers );
hook.Add( “PlayerDisconnected”, “playerscheck2”, checkforplayers );
[/lua]

Still need help, I made it like that and when players disconneted from my server the canstart value was still true with me being only person online.

When PlayerDisconnected the player hasn’t been removed from the server yet and he’s still on his team, so you’re always gonna have 2+ players when PlayerDisconnect is run. When in doubt, add prints to your code before where you think something’s going wrong so you can see what your variables look like.

So what are you suggesting I should do if I can’t use PlayerDisconnected?

bump

Use PlayerDisconnected, but make sure you account for the fact that the team’s playercount will always be one higher than you think because the player is still on the team when the hook is run

Oh my god I didn’t realize that… Thank you very much Luni!