Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with this code? I am trying to display the counter's value

Asked by 5 years ago
local counter = 100
    while(counter <= 100) do
    counter = counter - 1
    print(counter)
game.StarterGui.ScreenGui.countdown.Text = ("Countdown =" .. counter)
wait(1)
end

I am trying to have a textlabel displaying the countdown, and print it into the output as well. (I do have a textlabel in a ScreenGUI in Starter GUI named "countdown", so that is not the issue) the text label pops up, and it says "countdown = 99)

0
are u using a localscript? B_rnz 171 — 5y
0
use [player].PlayerGui rather than game.StarterGui theking48989987 2147 — 5y

3 answers

Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago

I found the problem (like the other people answering this question), and it's because you're trying to manipulate the text label inside the StarterGui. This wont work, as StarterGui is basically a service that clones anything within it into the player's PlayerGui. However, this is a pretty easy fix.

local Counter = 100

while (Counter <= 100) do
    Counter = Counter - 1
    script.Parent.countdown.Text = "Countdown = "..counter
end

This is the code except that it's slightly cleaner, and it's getting the countdown label. Make sure it's a local script located within the ScreenGui. For getting the countdown label I get the parent of the script (since it's probably in the screengui) and then I find the countdown label and manipulate the text to concatenate "Countdown = " and the value of the variable we declared previously, counter.

Ad
Log in to vote
0
Answered by
B_rnz 171
5 years ago

Use local scripts, or if you're using scripts, make sure the Parent of that script is 'textlabel'

Hope this works! lol

Log in to vote
0
Answered by
BuDeep 214 Moderation Voter
5 years ago

Your issue is you're referencing the games StarterGui, which acts as a placeholder for all your UI's.

However the items in this folder, will NOT show up on the players screen, unless they reset. This is because the players each have their own UI Folder, called "PlayerGui", which is where any items in the global StarterGui get cloned and put into.

So just tweak your code a bit and you should be fine :).

Answer this question