Repeat wait until ends instantly

The game is supposed to end if there’s 1 or less players alive or if 180 seconds have passed, but the game ends instantly instead. How do I fix it?

        local BattleValue = serverstorage:WaitForChild("Values"):WaitForChild("BattleValue").Value
        BattleValue = true

        coroutine.wrap(function()
			wait(180)
			print("Game timed out")
			BattleValue = false
		end)

		repeat
			wait()
			local playersAlive = Teams["Alive"]:GetPlayers()
			local BattleValue = serverstorage:WaitForChild("Values"):WaitForChild("BattleValue").Value
		until BattleValue == false or #playersAlive <= 1
1 Like

Hey, simply add this:

local gameStarted = tick()
repeat
   wait(.5)
until (tick() - gameStarted) > 180 or serverstorage:WaitForChild("Values"):WaitForChild("BattleValue").Value == false or #Teams["Alive"]:GetPlayers() <= 1

It checks every half a second if there’s 1 or less players, you can make it faster by changing the time in wait(). You can also pretty much delete the BattleValue unless you need it for any other scripts. Also don’t make it repeat every wait() since that could be a bit laggy, you can check every like half a second for count of players alive.

2 Likes

The actual reason for this error is that you are never modifying the BattleValue.Value property. Instead, you are just modifying the local variable, that you’ve named BattleValue. Here’s some fixed code:

        local BattleValue = serverstorage:WaitForChild("Values"):WaitForChild("BattleValue")
        BattleValue.Value = true

        coroutine.wrap(function()
			wait(180)
			print("Game timed out")
			BattleValue.Value = false
		end)

		repeat
			wait()
			local playersAlive = Teams["Alive"]:GetPlayers()
		until BattleValue.Value == false or #playersAlive <= 1
2 Likes