Object

非推奨を表示
作成できません
複製されていません

Object is the base class for all classes in the Roblox class hierarchy. Every other class that the Roblox engine defines inherits all of the members of Object. It is not possible to directly create Object.

概要

プロパティ

  • 読み取り専用
    複製されていません
    並列読み取り

    A read-only string representing the class this Object belongs to.

方法

イベント

プロパティ

ClassName

読み取り専用
複製されていません
並列読み取り

A read-only string representing the class this Object belongs to.

This property can be used with various other functions that are used to identify objects by type, such as Object:IsA() or Instance:FindFirstChildOfClass().

Note this property is read only and cannot be altered by scripts. Developers wishing to change an object's class will instead have to create a new Object.

Unlike Object:IsA(), ClassName can be used to check if an object belongs to a specific class ignoring class inheritance. For example:


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

This method returns an event that behaves exactly like the Changed event, except that it only fires when the given property changes. It's generally a good idea to use this method instead of a connection to Changed with a function that checks the property name. Subsequent calls to this method on the same object with the same property name return the same event.

ValueBase objects, such as IntValue and StringValue, use a modified Changed event that fires with the contents of their Value property. As such, this method provides a way to detect changes in other properties of those objects.

Note that this event will not pass any arguments to a connected function, so the value of the changed property must be read directly within a script.

Limitations

The event returned by this method does not fire for physics-related changes, such as when the CFrame, AssemblyLinearVelocity, AssemblyAngularVelocity, Position, or Orientation properties of a BasePart change due to gravity. To detect changes in these properties, consider using a physics-based event like RunService.PreSimulation.

Additionally, the returned event may not fire on every modification of properties that change very frequently, and/or it may not fire for such properties at all. It's recommended that you carefully test for property changes that impact game logic.

パラメータ

property: string

The property to connect to.


戻り値

A signal that fires whenever the property changes.

コードサンプル

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")
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 returns true if the object's class is equivalent to or a subclass of a given class. This function is similar to the instanceof operators in other languages, and is a form of type introspection. To ignore class inheritance, test the ClassName property directly instead. For checking native Lua data types (number, string, etc) use the functions type and typeof.

Most commonly, this function is used to test if an object is some kind of part, such as Part or WedgePart, which inherits from BasePart (an abstract class). For example, if your goal is to change all of a character's limbs to the same color, you might use GetChildren to iterate over the children, then use IsA to filter non-BasePart objects which lack the BrickColor property:


local function paintFigure(character, color)
-- Iterate over the child objects of the character
for _, child in character:GetChildren() do
-- Filter out non-part objects, such as Shirt, Pants and Humanoid
-- R15 use MeshPart and R6 use Part, so we use BasePart here to detect both:
if child:IsA("BasePart") then
child.BrickColor = color
end
end
end
paintFigure(game.Players.Player.Character, BrickColor.new("Bright blue"))

Since all classes inherit from Object, calling object:IsA("Object") will always return true.

パラメータ

className: string

The class against which the Object's class will be checked. Case-sensitive.


戻り値

Describes whether the Object's class matched or is a subclass of the given class.

コードサンプル

Instance:IsA()

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

イベント

Changed

This event fires immediately after an object property is changed and it works with most use cases (see limitations below). The new value of a changed property is not passed in as a parameter, so it must be accessed by using object[property]. For example:


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

If you are only interested in listening to the change of one specific property, consider using the GetPropertyChangedSignal() method instead.

For ValueBase objects such as IntValue and StringValue, this event only fires when the object's Value property changes. To detect other changes in ValueBase objects, use GetPropertyChangedSignal() instead.

Limitations

This event does not fire for physics-related changes, such as when the CFrame, AssemblyLinearVelocity, AssemblyAngularVelocity, Position, or Orientation properties of a BasePart change due to gravity. To detect changes in these properties, consider using a physics-based event like RunService.PreSimulation.

Additionally, this event may not fire on every modification of properties that change very frequently, and/or it may not fire for such properties at all. It's recommended that you carefully test for property changes that impact game logic.

パラメータ

property: string

The name of the property that changed.


コードサンプル

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"
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