Create.lua
Creates a new Instance with a list of properties, attributes,
tags and events. Based on the legacy RbxUtility.Create
method.
Creation flow:
- Create Instance (skipped when using Portals)
- Apply properties to the Instance
- Apply CollectionService tags to the Instance
- Reparent any child Instances to the new Instance
- Connect events, such as attribute change events, property
change events, and Instance events (such as
DescendantAdded
) - Set the Parent of the Instance
Warning: The Parent property is overridden for children. If a child specifies a Parent, it will be ignored, and the child will be parented to the parent in the
Create
tree.
Table of Contents
API Overview
Create<T>(className: string)(props: { [any]: any }) -> T
- ExampleCreate.Portal<T>(target: T, props: { [any]: any }) -> T
- ExampleCreate.Attribute(name: string) -> Symbol
- ExampleCreate.Event(name: string) -> Symbol
- ExampleCreate.Tag = Symbol
- ExampleCreate.Changed(property: string) -> Symbol
- ExampleCreate.Changed.Attribute(name: string) -> Symbol
- Example
Introduction
Create always returns a physical Instance. Props are static; updating the props will not update the Instance. If this is desired, you may be looking for libraries like Roact or Fusion.
local TextBox = Create "TextBox" {
ClearTextOnFocus = false,
BackgroundColor3 = Color3.fromHex("#1a1a1a"),
TextColor3 = Color3.fromHex("#fafafa"),
PlaceholderText = "Enter message",
Parent = PlayerGui.ScreenGui,
Text = "",
[Create.Changed "Text"] = function(rbx: TextBox, prev: string)
print("Text was changed from", prev, "to", rbx.Text)
end,
}
Tags and Attributes
Attributes and Tags can be applied to an Instance. Tags can either be a string or an array of strings.
local Create = require(path.to.create)
local Attribute, Tags = Create.Attribute, Create.Tag
Create "Configuration" {
Name = ".config",
[Attribute "Enabled"] = true,
[Tags] = { "Configuration", "PluginSettings" },
}
Events and Signals
Create can also connect events to listen for updates on attributes or properties, or connect Instance events.
local Changed, Event = Create.Changed, Create.Event
Create "TextBox" {
[Attribute "Enabled"] = true,
[Event "ChildAdded"] = function(rbx: TextBox, child: Instance)
print("Child", child.Name, "was added to TextBox", rbx.Name)
end,
[Changed "Text"] = function(rbx: TextBox, prev: string)
print("Text was changed from", prev, "to", rbx.Text)
end,
[Changed.Attr "Enabled"] = function(rbx: TextBox, prev: boolean?)
print("TextBox Enabled changed from", prev, "to", rbx:GetAttribute("Enabled"))
rbx.TextEditable = rbx:GetAttribute("Enabled")
end,
}
Children
Children can also be specified in the Create props.
Create "Frame" {
Size = UDim2.fromScale(1, 1),
Create "TextLabel" {
Text = "Hello!",
},
-- Reparent Baseplate to the new Frame
workspace.Baseplate,
}
Warning: The Parent property is overridden for children. If a child specifies a Parent, it will be ignored, and the child will be parented to the parent in the
Create
tree.
Constructors
Each Instance may have a single constructor method. This is called once the Instance has been created and all props have been applied (excluding Parent), and before events are connected.
local reference = nil
Create "Frame" {
BackgroundColor3 = Color3.fromHex("#00a2ff"),
[Create] = function(rbx: Frame)
reference = rbx
end,
}
Portals
Portals exist to allow you to bind events, properties and children to already existing Instances. This is good when you want to connect events to a service, for example:
local GuiService = game:GetService("GuiService")
local Lighting = game:GetService("Lighting")
local Portal, Event = Create.Portal, Create.Event
local blur = Create "BlurEffect" {
Size = 0,
Parent = Lighting,
}
Portal(GuiService) {
[Event "MenuOpened"] = function()
blur.Size = 24
end,
[Event "MenuClosed"] = function()
blur.Size = 0
end,
}