Consistent rate of countdown on a timer

I am trying to make a timer on a teleporter that teleports you to a game and when others get in, the timer keeps the same rate, counting down by 1 per second.

However, when two or more people get in, the timer goes down rapidly subtracting 1 from itself in less than second, and does not even keep a smooth rate.

I know the issue here since it “removes” 1 from the timer starting from the second of each time the part is hit and so on, but I am not sure of how to script it in order to keep a steady rate, counting down by 1 per second even though more players enter the queue.

Here is a part of the code where the issue occurs:
script.Parent.ins.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 < 4 then if game.StarterPlayer.StarterCharacterScripts.Leave.Value.Value == false then
				table.insert(list, char.Name)
				char.PrimaryPart.CFrame = spawnTeleport.CFrame

				updateGui()
				leaveGuiEvent:FireClient(player)
				
				lock:FireClient(player)
					

				while timer > -1 and #list > 0 do
					billboard.Frame.time.Text = timer
					wait(1)
							timer = timer - 1
					if timer == 3 then billboard.Frame.Status.TextColor3 = Color3.fromRGB(0, 255, 0)
						billboard.Frame.Status.Text = "READY"
					end
					if timer == 3 then baibai:FireClient(player)
					end
					if timer == 0 then billboard.Frame.Status.TextColor3 = Color3.fromRGB(255, 0, 0)
						billboard.Frame.Status.BorderSizePixel = 2
						billboard.Frame.Status.BorderColor3 = Color3.fromRGB(0, 0, 0)
						billboard.Frame.Status.Text = "TELEPORTING..."
					end
					if timer == 0 then
								script.Parent.Parent.door.close.tpval.Value = true
					end
				end
				
			

				if #list == 0 then
						timer = 15
						billboard.Frame.time.Text = timer
				end
				
				teleportPlayers()
				
			   end
					
			end

			player.CharacterRemoving:Connect(function(character)
				removeFromList(character)
			end)
		end
	end
end

end)

4 Likes

Disregard the fact that some of the script is outside of the box of where it’s supposed to be in. It’s still in the same order either way.

2 Likes

Sounds a lot like you got a connection being set whenever a player enters the queue, am I wrong?

You’re right. I am not sure how to make the timer keep a steady countdown rather than rapidly increasing when more players enter.

1 Like

May I see the portion of script above the block of code you are showing? I would like to see any events being connected, such as the touched event.

sure.
local TS = game:GetService(“TeleportService”)
local TweenService = game:GetService(“TweenService”)
local placeId = “17326217577”
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local billboard = script.hehehe.board.billboardGui
local timer = 15
local teleporting = false
local spawnTeleport = script.hehehe
local leave = game.StarterGui.MainGui.leaveButton
local baibai = game.ReplicatedStorage.baibai
local lock = game.ReplicatedStorage.lock
local egg = game.ReplicatedStorage.egg

local function updateGui()
billboard.Frame.players.Text = "Players: " …#list… “/4”
end

local function teleportPlayers()
if #list > 0 then
local playersToTeleport = {}
local teleportTime = 0
for i=1,#list do
if game.Players:findFirstChild(list[i]) then
table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
else
table.remove(list,i)
end
end
local code = TS:ReserveServer(placeId)
teleporting = true
TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
repeat wait() until #list <= 0
teleporting = false
end
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

2 Likes

It’s kinda hard to read with the current format. Could you send the whole script so I can see how it all works together?

Very well.

local TS = game:GetService(“TeleportService”)
local TweenService = game:GetService(“TweenService”)
local placeId = “17326217577”
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local billboard = script.hehehe.board.billboardGui
local timer = 15
local teleporting = false
local spawnTeleport = script.hehehe
local leave = game.StarterGui.MainGui.leaveButton
local baibai = game.ReplicatedStorage.baibai
local lock = game.ReplicatedStorage.lock
local egg = game.ReplicatedStorage.egg

local function updateGui()
billboard.Frame.players.Text = "Players: " …#list… “/4”
end

local function teleportPlayers()
if #list > 0 then
local playersToTeleport = {}
local teleportTime = 0
for i=1,#list do
if game.Players:findFirstChild(list[i]) then
table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
else
table.remove(list,i)
end
end
local code = TS:ReserveServer(placeId)
teleporting = true
TS:TeleportToPrivateServer(placeId,code,playersToTeleport)
repeat wait() until #list <= 0
teleporting = false
end
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.ins.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 < 4 then if game.StarterPlayer.StarterCharacterScripts.Leave.Value.Value == false then
				table.insert(list, char.Name)
				char.PrimaryPart.CFrame = spawnTeleport.CFrame

				updateGui()
				leaveGuiEvent:FireClient(player)
				
				lock:FireClient(player)
					

				while timer > -1 and #list > 0 do
					billboard.Frame.time.Text = timer
					wait(1)
							timer = timer - 1
					if timer == 3 then billboard.Frame.Status.TextColor3 = Color3.fromRGB(0, 255, 0)
						billboard.Frame.Status.Text = "READY"
					end
					if timer == 3 then baibai:FireClient(player)
					end
					if timer == 0 then billboard.Frame.Status.TextColor3 = Color3.fromRGB(255, 0, 0)
						billboard.Frame.Status.BorderSizePixel = 2
						billboard.Frame.Status.BorderColor3 = Color3.fromRGB(0, 0, 0)
						billboard.Frame.Status.Text = "TELEPORTING..."
					end
					if timer == 0 then
								script.Parent.Parent.door.close.tpval.Value = true
					end
				end
				
			

				if #list == 0 then
						timer = 15
						billboard.Frame.time.Text = timer
				end
				
				teleportPlayers()
				
			   end
					
			end

			player.CharacterRemoving:Connect(function(character)
				removeFromList(character)
			end)
		end
	end
end

end)

leaveGuiEvent.OnServerEvent:Connect(function(player)
game.StarterPlayer.StarterCharacterScripts.Leave.Value.Value = true
wait(1.1)
game.StarterPlayer.StarterCharacterScripts.Leave.Value.Value = false
end)

leaveGuiEvent.OnServerEvent:Connect(function(player)
if player.Character then
player.Character.HumanoidRootPart.Anchored = false
wait()
player.Character.Humanoid.Jump = false
wait()
player.Character:MoveTo(game.workspace.LeaveRoomPart.Position)
egg:FireClient(player)
removeFromList(player.Character) if #list == 0 then billboard.Frame.Status.TextColor3 = Color3.fromRGB(255, 0, 0)
billboard.Frame.Status.Text = “WAITING…”
end
end
end)

2 Likes

Ok so I had to paste this in a blank studio script so I could read it formatted then I could see the problem. It’s because each time a player touches the part, a new while loop is started. This begins to accumulate, causing the faster countdown.

1 Like

You can put the code responsible for looping the countdown outside of the touched event. Only put logic for adding new players to the queue in the Touched event.

The while loop will continue to operate as intended, while also allowing for players to enter the queue.

1 Like

That’s what I thought as well. But if I tried to separate the timer from the function, the client sided events would not work at all, and could affect the teleportation to the new game.

1 Like

Okay, I will try that in studio

1 Like

I apologize for the delayed response, but thank you so much. I’ve managed to put the looping code outside of the touching event, so queue adding and countdown is separate, as well as the remote events. The timer works perfectly now and has a consistent countdown, even with more or less players in the queue.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.