NumberValue

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

數字值是一個儲存單個 Luau 數字 的對象,定義為 雙精度浮點數字 或更常見的稱為 雙倍 。這裡使用 IEEE 754 表示(1 個符號位、11 個指數位和 52 個分數位)來儲存數字,使用 64 位(8 位)。可儲存的最大數值為 2^53、或 9,007,199,254,740,992,最小值為 -9,007,199,254,740,992。它可以存儲高達 15 位精度。

像所有 "-Value" 對象一樣,這個單值存儲在價值屬性中。對於這個 (以及其他像它一樣的對象) 所做的更改事件將使用新值存儲在對物件中,而不是使用代表被更改的屬性的字串。

範例程式碼

This sample demonstrates the subtleties of the Changed event on normal objects and "-Value" objects.

Changed Event

-- Demonstrate the Changed event by creating a Part
local part = Instance.new("Part")
part.Changed:Connect(print)
-- This fires Changed with "Transparency"
part.Transparency = 0.5
-- Similarly, this fires Changed with "Number"
part.Name = "SomePart"
-- Since changing BrickColor will also change other
-- properties at the same time, this line fires Changed
-- with "BrickColor", "Color3" and "Color3uint16".
part.BrickColor = BrickColor.Red()
-- A NumberValue holds a double-precision floating-point number
local vNumber = Instance.new("NumberValue")
vNumber.Changed:Connect(print)
-- This fires Changed with 123.456 (not "Value")
vNumber.Value = 123.456
-- This does not fire Changed
vNumber.Name = "SomeNumber"
-- A StringValue stores one string
local vString = Instance.new("StringValue")
vString.Changed:Connect(print)
-- This fires Changed with "Hello" (not "Value")
vString.Value = "Hello"

屬性

Value

平行讀取

用於保持雙倍值。

方法

活動

Changed

此事件會在 NumberValue.Value 屬性發生變更時發射。

這個事件,像其他已變更的事件一樣,可以用來跟蹤當數字值發生變更時以及可能變更的不同值。

例個體、實例,這甚至可能在依靠 NumberValues 來跟蹤遊戲狀態和值的遊戲中有用,例如物品ID。

相同類型的變更事件存在於類似物件,例如 ObjectValueStringValue,取決於哪種物件類型最適合需求。

參數

value: number

變更後的值。


範例程式碼

This example prints the NumberValue's new value each time it changes. Here it prints 20.

NumberValue Changed

local numberValue = script.Parent.NumberValue
local function printValue(value)
print(value)
end
numberValue.Changed:Connect(printValue)
numberValue.Value = 20