英文:
How do I set Text to NumberValue value?
问题
这是我正在尝试使文本始终设置为一个数值,但它不起作用。
这是我要讨论的代码...
while true do
wait(0.2)
script.Parent.Text = script.Parent.Parent.Amount.Value
end
我期望文本显示数值,但它没有。
英文:
So I'm trying to make the text always set to a numbervalue but it isn't working.
This is my code in question...
while true do
wait(0.2)
script.Parent.Text = script.Parent.Parent.Amount.Value
end
I was expecting for the text to say the number value but it didn't.
答案1
得分: 1
因为这在你的用户界面中,确保你正在使用的是LocalScript而不是Script。
而且一个好的方法是使用NumberValue上的Changed信号。它会在值更改时触发连接的函数,这样你可以轻松地保持文本属性与NumberValue的值同步。但是,因为该值是一个数字,请确保使用tostring
函数将其转换为字符串。
所以尝试这样做:
local numberValue = script.Parent.Parent.Amount
local label = script.Parent
-- 创建一个辅助函数来更新文本
function updateText()
label.Text = tostring(numberValue.Value)
end
-- 第一次设置文本
updateText()
-- 每次数字更改时更新文本
numberValue.Changed:Connect(updateText)
英文:
Since this is in your UI, make sure that you're using a LocalScript and not a Script.
And a good way to do this is to use the Changed signal on the NumberValue. It will trigger the connected function every time the value changes, so you can easily keep the text property synchronized with the NumberValue's value. But, because the value is a number, be sure to convert it to a string with the tostring
function.
So try this :
local numberValue = script.Parent.Parent.Amount
local label = script.Parent
-- create a helper function to update the text
function updateText()
label.Text = tostring(numberValue.Value)
end
-- set the text for the first time
updateText()
-- update the text every time the number changes
numberValue.Changed:Connect(updateText)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论