How to reset a timer and make it stop on a "story game"

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

end)

1 Like

Disregard parts of the script being outside of the box most of the script is in. The script is in order.

1 Like

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)
2 Likes

Thank you so much! This helped me understand the issue of my previous script, and taught me strategies I could use for future scripts!

Of course, If you have any more problems you can message me on here.

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