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
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
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())
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.
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