Timer script not working

Hello,
I made a timer server script but it does not work.
The timer stop at 29 seconds and nothing happens.

local Players = game:GetService("Players")
local Timer = 30

	task.wait(3)
repeat wait()
	if #Players:GetPlayers() > 2 then -- at least more than 2 players
	wait(1)
		Timer -= 1
		for i, player in pairs(Players:GetPlayers()) do
			player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Text = "Timer: " ..Timer
		end
	end
until Timer == 0

for i, player in pairs(Players:GetPlayers()) do
	player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Text = "Teleporting..."
	
	game:GetService("TeleportService"):Teleport(gameid here, player)
end

Does anyone know what’s the issue? - Thanks

change until Timer == 0 to until Timer < 0

1 Like

Use a RemoteEvent or a StringValue to display the text on screen

What’s that for?

Side note: don’t use wait(). Instead use task.wait(). It’s better but I don’t remember why.

Are there any errors in the code? (Swiggly lines or output)

Are there 3+ players while the timer is happening?

If you’re trying to get atleast 2 players you should use the >= operator.

Don’t use repeat when working with loops, use a while loop, and also use task.wait(1), also replace the > 2 with >= 2, else it only works for 3 players

while Timer > 0 and #Players:GetPlayers() >= 2 do
    task.wait(1)
    Timer -= 1
    if Timer <= 0 then
        -- teleport code here
    end
end
1 Like

Correct me if i’m wrong but isn’t it TeleportAsync? The ‘game id’ probably errors the script and the last argument is supposed to be an array

Try this (make sure to test in roblox player)

local Players = game:GetService("Players")
local Timer = 30
local placeId = 93248325 -- per example

	task.wait(3)
repeat wait()
	if #Players:GetPlayers() > 2 then -- at least more than 2 players
	wait(1)
		Timer -= 1
		for i, player in pairs(Players:GetPlayers()) do
			player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Text = "Timer: " ..Timer
		end
	end
until Timer == 0

for i, player in pairs(Players:GetPlayers()) do
	player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Text = "Teleporting..."
end

game:GetService("TeleportService"):TeleportAsync(placeId, Players:GetPlayers())

What if timer is 0? There would be a second delay if it’s Timer < 0 instead it should be Timer < 1 right?

It should be <= not just <. So timer should be less than or equal to 0

1 Like

Yes, when there are 2 players it works perfect.
But when there are more than at least it is not working

When i don’t put task.wait(3), the timer will not change. The text will say Timer: 30 and nothing will be changed

Forgot to say this will not work as Server Scripts don’t handle screenguis

1 Like

How can i make it handle screenguis? because when there are more than 2 players, it is not working for some reason

It’s not working. It says attempt to index nil with TextLabel.

You could fire a remote event to the players every time the timer counts down one, then have a local script inside of the timer text label, and have it change the timer that way.

1 Like

Is there a good reason for that? Repeat loops are pretty common in programming and there are plenty of valid cases to use them instead of while loops.

Try to add RemoteEvents in ReplicatedStorage

1 Like

For his problem using for loop would definetely better

	task.wait(3)
repeat wait()
	if #Players:GetPlayers() > 2 then -- at least more than 2 players
	wait(1)
		Timer -= 1
		for i, player in pairs(Players:GetPlayers()) do
			player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Text = "Timer: " ..Timer
		end
	end
until Timer == 0

vs

for i = 1, Timer do
   for i, player in pairs(Players:GetPlayers()) do
	  player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Text = "Timer: " ..Timer-i
   end
   task.wait(1)
end
1 Like