Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how do i make a timer using textlabel startergui?

if script.Parent.Parent.Started.Value == true then
	--timer script 5 min
end

2 answers

✓ Accepted answer

StarterPlayerScripts

local Player = game.Players.LocalPlayer
local Label = Player.PlayerGui:WaitForChild("ScreenGui").TextLabel
local InGame = 300

script.Parent.Parent.Started.Changed:Connect(function()
	if script.Parent.Parent.Started.Value == true then
	    for i=300,1,-1 do
	        InGame = InGame-1
	        if InGame < 10 then
	            Label.Text = "0:0"..tostring(InGame)
	        else if InGame < 60 then
	            Label.Text = "0:"..tostring(InGame)
	        else if InGame >= 60 then   
	                    local minutes = math.floor(InGame/60)
	                    local sec = InGame-minutes*60
	                    if sec < 10  then
	                        Label.Text = tostring(minutes)..":0"..tostring(sec)
	                    else
	                        Label.Text = tostring(minutes)..":"..tostring(sec)
	                    end
	                end
	            end
	        end
	        wait(1)
	    end
	end
end)







3 comments

0

script.Parent.Parent.Started.Changed:Connect(function() if script.Parent.Parent.Started.Value == true then end end)

0

do you have something like this?

The first half of the while loop will be formatting the time to look like an actual clock, with zeroes that fill in spaces that aren't being used. If the time was 4 minutes and 5 seconds, it would look like 04:05.

You'll need to figure out whether the number of seconds left is a single digit or a double digit. You can do this by determining whether or not it's greater than 9.

local Label = script.Parent
local Minutes = 5
local Seconds = 0

while true do
	if Seconds > 9 then
		if Minutes > 9 then
			Label.Text = Minutes .. ":" .. Seconds
		else
			Label.Text = "0" .. Minutes .. ":" .. Seconds
		end
	else
		
	end
end

Let me explain what lines 6-11 do. Line 6 checks to see whether or not the number of seconds is a single digit number. If it is, then on line 7 we need to figure out if the number of minutes is a single digit or a double digit number. Line 8 will run if the number of seconds and the number of minutes is a double digit number. We want the clock to have 4 digits, divided by a colon in the middle at all times, but since we have two numbers that are double digits, we can just concatenate the strings without adding any placeholder zeroes. Line 10 runs if the seconds are double digits, but not the minutes, so it places a zero before the number of minutes.

local Label = script.Parent
local Minutes = 5
local Seconds = 0

while true do
	if Seconds > 9 then
		if Minutes > 9 then
			Label.Text = Minutes .. ":" .. Seconds
		else
			Label.Text = "0" .. Minutes .. ":" .. Seconds
		end
	else
		if Minutes > 9 then
			Label.Text = Minutes .. ":0" .. Seconds
		else
			Label.Text = "0" .. Minutes .. ":0" .. Seconds
		end
	end
end

Lines 13-17 do almost the exact same thing, except we know that the number of seconds is a single digit, meaning we just have to add a zero before the seconds.

Now that the formatting is done, all we have to do is actually subtract seconds and minutes so it counts down.

local Label = script.Parent
local Minutes = 5
local Seconds = 0

while true do
	if Seconds > 9 then
		if Minutes > 9 then
			Label.Text = Minutes .. ":" .. Seconds
		else
			Label.Text = "0" .. Minutes .. ":" .. Seconds
		end
	else
		if Minutes > 9 then
			Label.Text = Minutes .. ":0" .. Seconds
		else
			Label.Text = "0" .. Minutes .. ":0" .. Seconds
		end
	end
end

if Seconds > 0 then
	Seconds = Seconds - 1
else
	if Minutes > 0 then
		Minutes = Minutes - 1
		Seconds = 59
	else
		break
	end
end

wait(1)

The way a clock works is it looks at the seconds first, sees if it can subtract a second, and subtracts a second if it can.

That's what line 21 is doing. If the number of seconds is greater than zero, then subtract 1 second.

If it IS zero, then run lines 24-29. Those lines will look at the minutes and make sure that we can subtract a minute. If we can, subtract a minute and set the seconds to 59. If not, that means the seconds is 0 and the minutes is 0, which means the timer has finished, and we can break.

And obviously, we have to wait 1 second for every second that is going to be subtracted.

I know there was another answer, but the person who posted it didn't go into detail about it.

3 comments

0

I already answered it

0

"I know there was another answer, but the person who posted it didn't go into detail about it."

0

Throwing a bunch of code at them that looks complicated is just going to overwhelm them.

This discussion is preserved as it was. New answers, comments, and votes are closed.