Countdown Timer Error

Hey guys, I hope you all do well! I’m making a countdown system, but for some reason, it goes faster by the “quantity” of the players in the server (2 Players = x2 times faster, 9 Players = x9 times faster).

I don’t want this to happen but for some reason it does.

I know there’s a lot of good people here, so I’ll be very grateful to you and your help if you can give me a hand with this, even if it’s a little one, thank you so much! Have a nice day! :wave: :grinning_face_with_smiling_eyes: :wink:

I’m on a LocalScript and then I fire a RemoteEvent but in the “for i = x, y, z do end” there is no for i, v in pairs(), so I really don’t know what’s happening.

Here’s the script:
(I don’t know if it’s something about the LocalScript or the Value that Changes)

  • LocalScript:
game.ReplicatedStorage:WaitForChild('ActivateRoundMoments').ActivateChoosingSystem.Changed:Connect(function()
	local player = game.Players.LocalPlayer
	local textLabel = player.PlayerGui:FindFirstChild('NightTest').RuleExplicationText
	textLabel.Visible = true	
	local function typewrite (object, text, length)
		for i = 1, #text,1 do
			object.Text = string.sub(text,1, i)
			wait (length)
		end
	end
	textLabel.Visible = false
	if textLabel.Visible == true then
		textLabel.Visible = not textLabel.Visible
	end
	wait(1)
	--Correctly place the Player Images and the Player Names on the UI--
	
	game.ReplicatedStorage:WaitForChild('RemoteEvents').PlaceGuiLocations:FireServer(player)
	
	--Correctly place the Player Images and the Player Names on the UI--
		
	--Play the Music--
		
	game.SoundService:WaitForChild('EnvironmentMusic').Music:Play()
		
	-- Play the Music--
	wait(3)
	--Activate the countdown--
	
    game.ReplicatedStorage:FindFirstChild('RemoteEvents').UpdateTimerGui:FireServer(player)
	
	--Activate the countdown--
	
	--Tween the BackgroundFrame down(With all the images of the players and their names to vote)--
	
	game.ReplicatedStorage:WaitForChild('RemoteEvents').ShowSelectionUI:FireServer(player)

	--Tween the BackgroundFrame down(With all the images of the players and their names to vote)--
	
end)
  • ServerScript (The countdown “OnServerEvent”):
game.ReplicatedStorage:WaitForChild('RemoteEvents').UpdateTimerGui.OnServerEvent:Connect(function(player)
	local Timer = game.ReplicatedStorage:WaitForChild('Timer')
	Timer.Value = 45	
	
	for i = 0, 45, 1 do
		wait(1)
		Timer.Value = Timer.Value - 1
		player:WaitForChild('PlayerGui').Timer.TitleFrame.SecondsLeft.Text = Timer.Value
	end

end)

I’m not sure this structure is ideal for a timer system. I think what’s happening is each time a player joins the game and that client code executes, a new timer basically begins, causing more and more remotes to get fired and therefor causing very frequent client updates (to make it seem like the timer is reducing faster, but really you just have multiple timers running.)

My suggestion would be to have the server completely handle your timer, you could do this by decrementing a NumberValue or something. Then, on the client, update a timer text label each time that timer value changes.

--//Client code

local someTimer = game.ReplicatedStorage:WaitForChild("Timer")

local function updateTimer()
script.Parent.Text = "Time left: "..someTimer.Value
end

updateTimer()
someTimer:GetPropertyChangedSignal("Value"):Connect(updateTimer)

And then your server-sided timer controller:

local START_COUNTDOWN = 45

local someTimer = game.ReplicatedStorage.Timer

local function runCountdown()

for i = START_COUNTDOWN, 0, -1 do
timer.Value = i

task.wait(1)
end
end
1 Like

I didn’t knew that you could make the Timer.Value to the “i” value, thank you so much for your help and for sharing your knowledge !

for i = 45, 0, -1 do
timer.Value = i

task.wait(1)
end

I wish you the best, have a nice day!

:grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes:

1 Like