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

Text label show's same text but different in the properties?

Asked by
CjayPlyz 643 Moderation Voter
5 years ago

Script in Workspace > Part

local text = workspace.Output.SurfaceGui.TextLabel.Text -- location of text

local function Clicked ()
    local number = math.random(1,10) -- makes a random number between 1 and 10
    print(number) -- prints the number
    text = number -- changes text to the number
    print(text) -- prints the text
end

script.Parent.ClickDetector.MouseClick:Connect(Clicked)

Output

2
2
6
6

1 answer

Log in to vote
1
Answered by 5 years ago

The issue here is that you just assigned the variable "text" to the text of the TextLabel. This means that even if you change "text" to something else, the text won't change, just the variable. This is very common for new developers. To do this, you just assign the textlabel itself to the variable, and not the text of it.

What do you do now is the same as doing local text = "Hello World" and if you change the text, it won't update the TextLabel.

You just have to change it to local text = workspace.Output.SurfaceGui.TextLabel

and whenever you want to update the text of it, you just do text.Text = "new string"

Personally, I like to name my variables something that makes sense, so I would rather use local textLabel and not local text in this case.

Basically,

local textLabel = workspace.Output.SurfaceGui.TextLabel;

-- logic
textLabel.Text = "New Text"
Ad

Answer this question