Need help making a minutes and seconds timer

  1. What do you want to achieve?
    I am trying to properly script a timer that counts down from 4:59 to 0:00. After the timer reaches 0:00 the player will get a small scene and get teleported back to the main lobby.

  2. What is the issue? I can get the timer to work if it is just a number value like 300 however I want it to read like a clock instead.

  3. What solutions have you tried so far? I am aware that there are posts around the forums however I haven’t seen one that fixes my solution, or at least the ones I have tried do not work. My knowledge of scripting is close to nil so perhaps I’m writing them wrong?

Either way, any and all help is welcomed.

This is the script I currently have, it is a Localscript placed inside the ServerScriptService folder.

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')

local minutes = 4

local seconds = 59

local function playGame()
	local timeAmount = minutes, seconds
	while timeAmount > 0 do
		timeEvent:FireAllClients(timeAmount)
		repeat
			if seconds <= 0 then
				minutes = minutes -1
				seconds = 59
			else
				seconds = seconds -1
			end
			if seconds < 10 then
				label.Text = tostring(minutes)..":0"..tostring(seconds)
			else
				label.Text = tostring(minutes)..":"..tostring(seconds)
			end
			wait(1)
		until minutes <= 0 and seconds <= 0
	end
end

local function resetPlayers()
	-- run cutscene event--
	for _, plr in pairs(game.Players:GetChildren()) do
		game:GetService("TeleportService"):Teleport(10142495757, plr)
	end 
end

while true do

	playGame()
	resetPlayers()

end

I have a second LocalScript inside of the timer text gui itself that looks like this

local label = script.Parent


local ReplicatedStorage = game:GetService('ReplicatedStorage')
local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')


timeEvent.OnClientEvent:Connect(function(timeAmount)
	label.Text = timeAmount

end)

and a remote event that fires all of this to the players from ReplicatedStorage.

1 Like

This should work for what your looking for if I am not mistaken.

Might need changing depending on how you want it to work.

unfortunately that doesn’t really help me in this case. I wouldn’t know where to put the string of code provided into my own code since I need it to do an extra step of kicking the player off the server.

Local Script will not work in ServerScriptService the name is "ServerScript"Service and while true do is bad, if u make the Localscript to ServerScript your Pc will lag because while true do is a bad thing to do without wait.

Alright I copied all the information into a regular script but I still keep getting an error on line 23. Nothing as far as I know is spelled incorrectly?

I made my own better Code:

Put this inside a ServerScript (Not LocalScript) and the ServerScript to ServerScriptService:

local minutes = 4

local seconds = 59

local function playGame()
	
	repeat
		if minutes > 0 and seconds == 0 then
			
			minutes -= 1
			seconds	= 59
			
		elseif minutes >= 0 and seconds > 0 then
			
			seconds -= 1
			
		end
		
		game.ReplicatedStorage:WaitForChild("RemoteEvent"):FireAllClients(minutes, seconds)
		
		task.wait(1)
		
	until minutes == 0 and seconds == 0
	
end

playGame()

U need to fire PlayGame() one Time if u want to start the Timer.

Put this inside LocalScript then put the LocalScript under your Label:

game.ReplicatedStorage:WaitForChild("RemoteEvent").OnClientEvent:Connect(function(minutes, seconds)
	
	if minutes < 10 and seconds < 10 then
	
		script.Parent.Text = "0"..minutes..":0"..seconds..""
		
	elseif minutes >= 10 and seconds >= 10 then
		
		script.Parent.Text = ""..minutes..":"..seconds..""
		
	elseif minutes >= 10 and seconds < 10 then
		
		script.Parent.Text = ""..minutes..":0"..seconds..""
		
	elseif minutes < 10 and seconds >= 10 then
		
		script.Parent.Text = "0"..minutes..":"..seconds..""
		
	end
	
end)

And why do u need:

while true do

	playGame()
	resetPlayers()

end

This code over me makes no sense to me.

Ah, firstly I’d like to thank you a ton! Your code is working perfectly!

I’m not really sure why I needed the while true loop, it was in the video tutorial I found and it seemed like the best way to make sure the teleport actually worked in case the teleport failed.

Thank you again!

1 Like

Tmm no problem!


If u want to teleport the Player after the Timer u can add the Teleport Code under until minutes == 0 and seconds == 0

1 Like

Oh thank you again! I almost forgot about that part haha!

1 Like