Object

顯示已棄用項目

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

無法建立
未複製

對象是 Roblox 類別階層中的所有類別的基本類別。 所有其他類別,由 Roblox 引擎定義,都會繼承所有對象的成員。 不是直接創建對象的。

概要

屬性

  • 唯讀
    未複製
    平行讀取

    一個只讀取Class的字串代表Class的來源。

方法

活動

屬性

ClassName

唯讀
未複製
平行讀取

一個只讀取Class的字串代表Class的來源。

這個屬性可以與多種其他用於識別對象類輸入的功能,例如 Object:IsA()Instance:FindFirstChildOfClass()

注意此屬性只閱取,並且不能由腳本變更對物件的類別。開發人員想要變更對象的類別的話,會相反需要建立新的 Object

Object:IsA() 不同,ClassName 可以用來檢查對象是否屬於特定類別,無視類別継承。例如:


for _, child in workspace:GetChildren() do
if child.ClassName == "Part" then
print("Found a Part")
-- will find Parts in model, but NOT TrussParts, WedgeParts, etc
end
end

方法

GetPropertyChangedSignal

此方法返回一個與 Changed 事件相同的事件,除了它只會在指定的屬性變更時僅發生外。它通常是一個好主意使用此方法而不是連接到 Changed 使用功能檢查屬性名稱。後續對此方法在同一個對象上使用相

Class.ValueBase 個,例如 Class.IntValue 和 Class.StringValue ,使用修改後的 0> Class.Object.Changed|Changed 事件發射其內容的 ValueBase3>。因此,此方法提供一種方法來偵測這些對象的其他屬性。

注意,這個事件不會傳送任何參數給連接的函數,因此變更的屬性必須在指令碼中直接閱取。

限制

這個方法的返回事件不會發生物理相關變更,例如當 Class

此外,返回的事件可能不會在每次變更屬性變更非常頻繁時發生,並且/或在此類屬性上發生。建議您仔細測試發生遊戲邏輯影響的屬性變更。

參數

property: string

連接到的屬性。


返回

一個信號,當屬性變更時發生。

範例程式碼

This code sample demonstrates how to save a value before a changed event fires on it in order to get more information about a change.

Old-to-New Values with Changed

local part = Instance.new("Part")
local currentColor = part.BrickColor
local function onBrickColorChanged()
local newColor = part.BrickColor
print("Color changed from", currentColor.Name, "to", newColor.Name)
currentColor = newColor
end
part:GetPropertyChangedSignal("BrickColor"):Connect(onBrickColorChanged)
part.BrickColor = BrickColor.new("Really red")
part.BrickColor = BrickColor.new("Really blue")

This code sample demonstrates the equivalence of the Changed event and event returned by GetPropertyChangedSignal.

Changed and GetPropertyChangedSignal

local part = Instance.new("Part")
local function onBrickColorChanged()
print("My color is now " .. part.BrickColor.Name)
end
local function onChanged(property)
if property == "BrickColor" then
onBrickColorChanged()
end
end
part:GetPropertyChangedSignal("BrickColor"):Connect(onBrickColorChanged)
part.Changed:Connect(onChanged)
-- Trigger some changes (because we connected twice,
-- both of these will cause two calls to onBrickColorChanged)
part.BrickColor = BrickColor.new("Really red")
part.BrickColor = BrickColor.new("Institutional white")

IsA

平行寫入

IsA 返回 字串rue 如果對物件的類別是 相當於 或提供的一個 子類 。 此功能與其他語言中的 類型檢查器 類似,並且是一

最常見的情況是,這個函數通常用來測試對象是否是某種零件,例如 PartWedgePart,這


local function paintFigure(character, color)
-- 在角色的子對象上迭代
for _, child in character:GetChildren() do
-- 過濾非零件物件,例如襯衫、褲子和人形
-- R15 使用 MeshPart 和 R6 使用 Part,因此我們在這裡使用 BasePart 來偵測兩者:
if child:IsA("BasePart") then
child.BrickColor = color
end
end
end
paintFigure(game.Players.Player.Character, BrickColor.new("Bright blue"))

因為所有類別都從 Object 継承,所以呼叫 object:IsA("Object") 將永遠返回真。

參數

className: string

對象的類別將被檢查的類別。 對象敏感。


返回

描述對象是否匹配或是否是指定類的子類。

範例程式碼

Demonstrates determining the class of Workspace using Instance:IsA()

Workspace is of class 'Workspace', so the first statement is true. Workspace is not of class 'BasePart', so the second statement is false. Workspace inherits from Instance and therefore is of class 'Instance', so the third statement is true.

Instance:IsA()

local Workspace = game:GetService("Workspace")
print(Workspace:IsA("Workspace")) -- true
print(Workspace:IsA("BasePart")) -- false
print(Workspace:IsA("Instance")) -- true

活動

Changed

這個事件會在一個對象屬性變更後立即發生,並且與大多數使用案例 (請參閱限制下方) 工作。變更的屬性值是 通過參數傳递,因此必須使用 object[property] 來存取。例如:


object.Changed:Connect(function(property)
print("The new property's value is", object[property])
end)

如果您只有興趣聆聽一個特定屬性的變更,請考慮使用 GetPropertyChangedSignal() 方法。

對於 ValueBase 對象,例如 IntValueStringValue,此事件只會在對物件的 1> Value1> 屬性變更時僅發生。若要偵測其他變更在 4> Class.ValueBase

限制

此事件不會發生於物理相關變更,例如當 Class.BasePart.CFrame|CFrame

此外,此事件可能不會在每次更改屬性變更非常頻繁時發生,並且/或者它可能不會發生此全部 所有屬性。建議您仔細測試發生遊戲邏輯影響的屬性變更。

參數

property: string

變更名稱。


範例程式碼

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"

This code sample demonstrates the Changed event firing within a parent object.

Change Detector

local object = script.Parent
local function onChanged(property)
-- Get the current value of the property
local value = object[property]
-- Print a message saying what changed
print(object:GetFullName() .. "." .. property .. " (" .. typeof(value) .. ") changed to " .. tostring(value))
end
object.Changed:Connect(onChanged)
-- Trigger a simple change in the object (add an underscore to the name)
object.Name = "_" .. object.Name