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

Updating a text label when a value changes?

Asked by 10 years ago

Here is what I tried.. but it lead me to another question..

while true do
   wait(1)
script.Parent.CurrencyFrame.Money.Text = "$".. script.Parent.CurrencyFrame.Money.Amount.Value

end)

I am trying to update the text label to the value of the amount script.Parent.CurrencyFrame.Money.Amount.Value

But also always have a $ sign a front of the number. How would I make it so it formats correctly?

2 answers

Log in to vote
2
Answered by 10 years ago

You can just use the Changed event of the Value object. Every time it fires, update the TextLabel text. Simple as that!

local label = script.Parent.CurrencyFrame.Money
local value = label.Amount

value.Changed:connect(function()
    label.Text = "$" .. value.Value
end)

Note that we set the variables to the objects themselves, not the properties. If we would have set it to the properties, the variable would become the primitive datatype that the property is and would not reference the property in the object. If you don't know what that means, look at this:

local part = Workspace.Part
part.Name = "Hello"

local a = part.Name

print(a)
--> Hello

part.Name = "Goodbye"

print(a)
--> Hello

print(part.Name)
--> Goodbye
1
This helped me a lot. Thanks! IntellectualBeing 430 — 10y
Ad
Log in to vote
2
Answered by
Andalf 100
10 years ago

First of all, while loops are generally bad, so let's find your value and use the .Changed event

local money = script.Parent.CurrencyFrame.Money.Amount

next we attach an anonymous function to it like this.

money.Changed:connect(function() end)

between that we can then run any code, in your case set the text

money.Parent.Text = "$"..money.Value

putting this altogether gets you this.

local money = script.Parent.CurrencyFrame.Money.Amount
money.Parent.Text = "$"..money.Value

money.Changed:connect(function()
    money.Parent.Text = "$"..money.Value
end)
1
I wish I could accept two answers, because this one is correct as well. IntellectualBeing 430 — 10y
0
This is a good answer, Andalf, but for the sake of people seeing this response later, I wouldn't say that while loops are generally bad. In this case it's much more efficient to use an event as you describe, but while loops are not inherently bad. AxeOfMen 434 — 10y

Answer this question