Getting numbers to Display on TextLabel

  1. I’m trying to make an countdown that is repeating and It should display on textlabel.

  2. So I don’t know how to get the function to display on on textlabel if it’s possible.

  3. I couldn’t find anything else.

local b = true
local ScreenGUI = game.StarterGui:WaitForChild("ScreenGui")
local text = ScreenGUI:WaitForChild("TextLabel")

function timer()	
	for a = 1, 10 do	
		print(a)
		wait(1)
	end
end

text.Text = timer()

repeat timer()
until b == false

I still have lot to learn:)

2 Likes

Don’t think you can place functions to be “shown” on TextLabels. Perhaps make it such that the function changes its text to the Value “a”.
(Snippet of code that you should edit):

function timer()	
  for a = 1, 10 do	
  print(a)
  text.Text = a
  wait(1)
  end
end

You can only set something equal to a function when it returns something. In your example, setting the text = timer() won’t do anything because timer() doesn’t return anything. Instead, just change the text each time the timer goes down.

local b = true
local ScreenGUI = game.StarterGui:WaitForChild("ScreenGui")
local text = ScreenGUI:WaitForChild("TextLabel")

function timer(startValue)	
	for a = startValue, 0, -1 do
		print(a)
		text.Text = tostring(a)
		task.wait(1)
	end
end

timer(100) -- will count down from 100

Unlike some of the other replies that essentially give you the script, I’ll show you how to make it. I like your original script, you were very close and almost there.

I assume by timer, you want it to countdown from a number, you can define that number with local timerNumber = 100. In this example, we will make the timer for 100 seconds.

Next you will need to create a function to countdown, you have declared a function in your script so we will need to do that again with local function timer(). Now a function is created and so far everything we have is this:

local timerNumber = 100

local function timer()
	
end

You will now need to put all the code inside the function to make it do what you want. If you think about what we are trying to achieve, we essentially need this script to countdown from 100 and then stop at 0. This can be done by using a while true do loop and checking each loop if timerNumber is equal to 0. Lets put this code inside the function.

Now we have the following:

local timerNumber = 100

local function timer()
	while true do
		wait(1)
		timerNumber -= 1
		if timerNumber == 0 then
			
		end
	end
end

You will notice that the loop has a wait() inside of it. Always put a wait() inside of loops otherwise they overload and stop working. In this situation, we want to wait 1 second before subtracting each number so that one second is actually one second. You will also see timerNumber -= 1. If you expand this line, what that essentially is saying is timerNumber = timerNumber - 1 but in a shorter form. I have achieved this by doing -= instead.

So far we have the countdown number, the function, the loop, the subtraction part, and the checker. Always remember that if you are trying to check the value of something, use double equal signs (==).

We are basically done now, this is a timer that will count down from 100 until it hits 0 in which case it will stop. However, we haven’t told it to stop yet. We can tell it to stop by using the break feature. By putting the word break after if timerNumber == 0 then we are telling the script to break the loop.

Right now, this is everything that we have:

local timerNumber = 100

local function timer()
	while true do
		wait()
		timerNumber -= 1
		if timerNumber == 0 then
			break
		end
	end
	
	print("Finished!")
end

“Finished” will print after everything has completed. You will notice that everything is finished now except displaying it on the TextLabel. This can be done by simply inserting a script.Parent.Text = timerNumber into the while true do loop. If this script is a child of the textlabel, script.Parent.Text should work, otherwise you will need to define the TextLabel.

Now the verrry last thing to do is to call the function at the bottom with timer(). Now everything we have is

local timerNumber = 100

local function timer()
	while true do
		wait(1)
		timerNumber -= 1
		if timerNumber == 0 then
			break
		end
		script.Parent.Text = timerNumber
	end
	
	print("Finished!")
end

timer()

I hope this helps! If you have any problems with scripting regarding this issue or anything else, please do not hesitate to contact me either through devforum or another platform. I would be happy to help.

1 Like