Loop breaks even though it doesn't fit criteria

I’m not fond of the spawn(function()) as it creates a separate thread and it’s setting a variable constantly.

You could have the code execute a repeat statement, e.g:

local runners = #game.Players:GetPlayers();

repeat 
    wait() 
    runners = #game.Players:GetPlayers();
until runners < 1 or won.Value == true

Then you can determine what happens next. This will pause the current thread until one of the coditions are met.

1 Like

don’t place that inside that, just leave that part as

for i = roundLength, 0, -1 do
		InRound.Value = true		
		if won.Value == true then
			break
		end
		wait(1)
		status.Value = "Game: ".. i .." seconds left!"
                       if #runnersTable < 1 then break end
	end

it should work precisely like that because when all users die and the game timer is still running it will realize that the runnersTable is < 1 and will break

1 Like

@Exeplex I put this:

			repeat 
				wait() 
			 runners = #game.Players:GetPlayers();
			until runners < 1 or won.Value == true

Inside of this:

for i = roundLength, 0, -1 do
			InRound.Value = true		
			if won.Value == true then
				break
			end
			wait(1)
			status.Value = "Game: ".. i .." seconds left!"
		end

It didn’t work. Intermission clock would always stay at zero.

@greatneil80 Ok it now tells me runnersTable is an unknown variable blah blah, what would I set it as? I deleted it earlier, but now I want to hear you say what it should be.

1 Like

No, replace the for loop and remove the spawn(function()) and try this instead:

local runners = #game.Players:GetPlayers();
local roundlength = (Your length)

repeat 
    runners = #game.Players:GetPlayers();
    roundlength = roundlength - 1;
    status.Value = "Game: ".. roundlength.." seconds left!"
    wait(1) 
until runners < 1 or won.Value == true or roundlength <= 0

if (runners < 1) then
    -- Do something
elseif (won.Value == true then)
    -- Do something
end

If that doesn’t fix your issue, it’s not a synchronizing issue. Check what game.Players:GetPlayers() is returning with a print(#game.Players:GetPlayers()) and check the won.Value with a print(won.Value) inside of the repeat function.

Edit: I forgot to add the roundlength <= 0 check to break the until loop as well.

1 Like

just keep it as the same thing, restore the original script you started out with but just change it with that one thing I told you earlier

1 Like

I restored my original script, and all I changed was the original for loop to this:

for i = roundLength, 0, -1 do
		InRound.Value = true		
		if won.Value == true then
			break
		end
		wait(1)
		status.Value = "Game: ".. i .." seconds left!"
                       if #runnersTable < 1 then break end
	end

And I think I made a little advancement! It would count down intermission, but when it reached game time it would teleport the players to the game area for a tiny fraction of a second and go back to intermission spawn and restart intermission time. What now?

Edit: When the loop breaks, it’s supposed to go back to intermission time. Maybe I need to add a wait? But where?

1 Like

add InRound.Value = false after that line before the end, it should correctly adjust the if statement

1 Like

Here’s what I did. I put inround = false like this:

if #runnersTable < 1 then 
    break 
end
InRound.Value = false

When I did it, it would do what it did earlier (blip into the game time for a second as well as teleport players then go back). When I did it like this:

if #runnersTable < 1 then 
    InRound.Value = false
    break 
end

It would not even blip, just loop intermission.

1 Like

isn’t that what you want? ____

1 Like

Not exactly. I test the game as 2 clients with my computer acting as a local server, so there is 1 player per team.

In any language, local variables will be faster.
You will need to understand what a register is and what the thread stack is to understand my explanation.
Most local variables are implemented as a register variable or pushed near the top of the local stack, so they are generally accessed much more quickly.
Global variables are stored further up the stack (if they are not on the heap) so computing their address to access them is slower.

1 Like

Exeplex has solved this in team create. Thank you all for the help.

1 Like