Timer does not work well

Hello, I am doing a timer and I want to make that when there is only one player in the game, the text will be “There are not enough players”. But when the intermission comes to an end and there are more than 1 player, “Not enough players” is displayed and then the text I want. Why is this happening?
Code:

local intermission = 5

local String = game.ReplicatedStorage.Round.String
local InRound = game.ReplicatedStorage.Round.InRound

local function roundTimer()
	while wait() do
		for i = intermission, 1, -1 do
			InRound.Value = false
			wait(1)
			String.Value = "Intermission: ".. i .." Seconds left!"
		end
		local playerTable = game:GetService("Players"):GetPlayers()
		for i, v in pairs(playerTable) do
			print(i)
			if i == 1 then
				wait(1)
				String.Value = "Not enough players!!"
			elseif i >= 2 then			
				wait(1)
				local randomPlayer1 = playerTable[math.random(1, #playerTable)] -- selecting our first random player

				table.remove(playerTable, table.find(playerTable, randomPlayer1)) -- removing the player we got in the above statement, from the table

				local randomPlayer2 = playerTable[math.random(1, #playerTable)] -- selecting our second random player
				String.Value = randomPlayer1.Name.." and "..randomPlayer2.Name.." was chosen!!"
			end
		end
		

		wait(5)
	end
end

roundTimer()

video:

1 Like

Right now you’re looping through all the values and indexes of the players within the game, and checking the player count with i. I, in your case, whenever the loop starts, it starts from 1 as per usual. This means that it registers the if statement seeing if it’s == 1, which it is, making it output not enough players. To fix this, instead of doing a loop, instead get the length of the players returned by the GetPlayers method by doing #playerTable and check if that is <= 1.

I went ahead and redid some of your script. The if statement you were using was not correct. In fact, you didn’t even need to use one.

When you’re using an if statement, remember that else is always going to be ran when none of the parameters were met. Example:

if #players >= 2 then
     -- only runs when the #players >= 2
else
     -- In this case the #players was not >= 2 therefor it must be <= 1
end

I also went ahead and added a repeat loop. This will remove the if statement entirely. Here is what it looks like:

repeat task.wait()  -- repeating will do a constant loop until the requirement is met

	String.Value = "Not enough players!"

until #playerTable >= 2

-- Nothing down here will run until the #playerTable >= 2 

It basically just makes it so you don’t have to use an if statement, and it will continue to halt the script until there’s more than 2 players.

Another thing, roblox rolled out the task library. I swapped out all of your old waits with task.wait. It’s more efficient regarding the scheduling.

This code worked for me --in game-- so im hoping it works for you as well. Just make sure you understand the changes I made so that way you learn from the mistakes.

Revised Code
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local intermission = 5

local String = replicatedStorage:WaitForChild("String")
local InRound = replicatedStorage:WaitForChild("InRound")

local function roundTimer()
	task.spawn(function() -- Spawn, remove if you need, it just makes it so this while loop doesn't hault the rest of the code
		while task.wait() do
			for i = intermission, 0, -1 do
				InRound.Value = false
				task.wait(1)
				String.Value = "Intermission: ".. i .." Seconds left!"
			end
			
			task.wait(1)
			
			local playerTable = players:GetChildren()

			repeat task.wait()  -- repeating will do a constant loop until the requirement is met

				String.Value = "Not enough players!"

			until #playerTable >= 2

			String.Value = "Selecting player(s)..."

			task.wait(1)
			
			local randomPlayer1 = playerTable[math.random(1, #playerTable)] 
			table.remove(playerTable, table.find(playerTable, randomPlayer1))
			
			local randomPlayer2 = playerTable[math.random(1, #playerTable)]
			String.Value = randomPlayer1.Name.." and "..randomPlayer2.Name.." were chosen!"

			task.wait(5)
		end
	end) -- if you removed the spawn remove this end.
end

roundTimer()

Thank you! I learned many things thanks to you, I appreciate it

1 Like