Hello, I am currently making a timer in a story game that allows you to teleport to the game itself along with a queue of 3 players, and I am trying to make the timer stop and reset to the seconds it starts with once there are no players in queue.
Unfortunately, when I exit the queue, and there aren’t any players in queue, the timer keeps on counting down to the negatives and does not stop.
Here is a part of the script that involves the timer:
script.Parent.Part69.Touched:Connect(function(hit)
if hit.Parent:findFirstChild(“Humanoid”) then
if teleporting == false then
local char = hit.Parent
local player = game.Players:FindFirstChild(char.Name)
local inqueue = false
for i=1,#list do
if list[i] == char.Name then
inqueue = true
end
end
if inqueue == false then
if #list < 3 or #list == 3 then
table.insert(list,char.Name)
char.PrimaryPart.CFrame = spawnTeleport.CFrame
updateGui()
leaveGuiEvent:FireClient(player)
timer = 15
for i=1,timer do
timer = timer - 1
billboard.Frame.time.Text = timer
wait(1)
end
teleportPlayers()
end
player.CharacterRemoving:connect(function(character)
removeFromList(character)
end)
end
end
end
Sounds like this game will be great. To fix this you must follow this. Modify to your likings
local timer = 15 -- Initial timer value
local list = {} -- Queue of players
local function updateGui()
end
local function removeFromList(character)
for i, playerName in ipairs(list) do
if playerName == character.Name then
table.remove(list, i)
updateGui()
break
end
end
end
script.Parent.Part69.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if not teleporting then
local char = hit.Parent
local player = game.Players:FindFirstChild(char.Name)
local inqueue = false
for i = 1, #list do
if list[i] == char.Name then
inqueue = true
end
end
if not inqueue then
if #list < 3 or #list == 3 then
table.insert(list, char.Name)
char.PrimaryPart.CFrame = spawnTeleport.CFrame
updateGui()
leaveGuiEvent:FireClient(player)
timer = 15
while timer > 0 and #list > 0 do
billboard.Frame.time.Text = timer
wait(1)
timer = timer - 1
end
if #list == 0 then
timer = 15
end
teleportPlayers()
end
player.CharacterRemoving:Connect(function(character)
removeFromList(character)
end)
end
end
end
end)