Skip to content

Component

Type

Component reads and writes the fields of one component on an entity. Index an entity with the name of a component that has fields, as in entity.Timer, to get one; the result is nil when the entity does not have that component. Index the result with a field name to read or write that field. A single-value component such as Position returns its value instead. A built-in component uses the field names in Component views.

__iter

Function
__iter(self: Component) -> (Component) -> (string, any)

__iter visits every field of the component in declaration order, with its name and current value. The type checker does not see this iterator on component views, so a strict script casts the component to any to loop over it.

Example

-- Server script in a part.
self:Add(Component.Timer)
local timer = self.Timer
if timer then
    timer.WaitTime = 5
    for name, value in timer :: any do
        print(name .. " = " .. tostring(value))
    end
end
-- prints WaitTime = 5, then TimeLeft = 0