如何将文本设置为NumberValue值?

huangapple go评论73阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年7月11日 10:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定