Problem with a Timer System

Hey, so i’m having an issue with a timer system right now,
and i’m looking for help.


Issue Context

So basically what I did, is I made a function, with a countdown in it.
And when it hits 0, I called the function again so it keeps looping.
However, when the timer restarts, it goes 2x faster. (which is the issue)

EDIT : I just realised it speeds up when I die.


Script

local function StartGame(plr)

	--// Variables
	isTeleported = false
	plr:WaitForChild("leaderstats"):WaitForChild("MapVoted").Value = ""
	values:WaitForChild("GameLength").Value = defaultGameLength
	values:WaitForChild("IntermissionLength").Value = 0
	local character = plr.Character
	local humRP = character:WaitForChild("HumanoidRootPart")
	local plates = workspace:WaitForChild("Lobby"):WaitForChild("VotingPlace"):WaitForChild("Plates")
	local blocksVotes = plates:WaitForChild("Blocks"):WaitForChild("VotesAmount")
	local cityVotes = plates:WaitForChild("City"):WaitForChild("VotesAmount")
	local forestVotes = plates:WaitForChild("Forest"):WaitForChild("VotesAmount")
	local skylandsVotes = plates:WaitForChild("Skylands"):WaitForChild("VotesAmount")
	
	--// Game Countdown
	for count1 = values:WaitForChild("GameLength").Value, 0, -1 do
		
		--// If it's not over
		if values:WaitForChild("GameLength").Value > 0 then
			values:WaitForChild("GameLength").Value -= 1
		end
		
		--// If it's over
		if values:WaitForChild("GameLength").Value <= 0 then
			values:WaitForChild("IntermissionLength").Value = defaultIntermissionLength
			
			--// Intermission Countdown
			for count2 = values:WaitForChild("IntermissionLength").Value, 0, -1 do
				
				--// If it's not over
				if values:WaitForChild("IntermissionLength").Value > 0 then
					values:WaitForChild("IntermissionLength").Value -= 1
					if not isTeleported then
						isTeleported = true
						humRP.CFrame = workspace:WaitForChild("Lobby"):WaitForChild("SpawnLocation").CFrame
					end
				end
				
				--// If it's over
				if values:WaitForChild("IntermissionLength").Value <= 0 then
					values:WaitForChild("GameLength").Value = defaultGameLength
					if blocksVotes.Value > cityVotes.Value and blocksVotes.Value > forestVotes.Value and blocksVotes.Value > skylandsVotes.Value then
						chosenMap.Value = blocksVotes.Parent.Name
					end
					if cityVotes.Value > blocksVotes.Value and cityVotes.Value > forestVotes.Value and cityVotes.Value > skylandsVotes.Value then
						chosenMap.Value = cityVotes.Parent.Name
					end
					if forestVotes.Value > cityVotes.Value and forestVotes.Value > blocksVotes.Value and forestVotes.Value > skylandsVotes.Value then
						chosenMap.Value = forestVotes.Parent.Name
					end
					if skylandsVotes.Value > forestVotes.Value and skylandsVotes.Value > cityVotes.Value and skylandsVotes.Value > blocksVotes.Value then
					chosenMap.Value = skylandsVotes.Parent.Name
					end
					humRP.CFrame = workspace:WaitForChild(chosenMap.Value):WaitForChild("TeamSpawns"):WaitForChild(plr.Team.Name).CFrame + Vector3.new(0,10,0)
					remotes:WaitForChild("ClientReplicator"):FireAllClients()
					break
				end
				
				wait(1)
			end
			
			break
		end
		wait(1)
	end
end

task.spawn(function()
	wait(2)
	remotes:WaitForChild("StartGame").OnServerEvent:Connect(StartGame)
end)

Thanks for helping!

1 Like

are you firing the event on spawn?

thats because something is firing the function twice and the countdown relies on a IntValue (i believe) to determine if its over or not, instead of using the value to the count1 for checks

1 Like

First thing use elseif and do not create only if’s

1 Like

still would be tedious and slow to do, i recommend a for loop with math.max to get the highest votes

1 Like

also use a moduel script to detect wich is the highest vote that will make your life eazyer

1 Like

i dont see a line calling StartGame so i’d assume that you’re firing the event after respawning; which we dont have the information of when and where its fired

1 Like

You should not connect remote events like this, as it will cause a memory leak.

2 Likes