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 Hydration)
- 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.
- Install
- API Overview
- Introduction
- Tags and Attributes
- Events and Signals
- Children
- Constructors
- Hydration
- Unstable Version
Download and insert, or copy and paste Create.lua into a ModuleScript in Studio.
Paste and run the following line of code in Studio's command bar.
local hs=game:GetService"HttpService";local he=hs.HttpEnabled;hs.HttpEnabled=true;local src=hs:GetAsync"https://gist.github.com/cxmeel/2fd7092ed359fc769abc56a38ec74441/raw/Create.lua";local m=Instance.new"ModuleScript";m.Name="Create";m.Source=src;m.Parent=game:GetService"ReplicatedStorage";hs.HttpEnabled=he
Command overview
- Get the current state of
HttpService.HttpEnabled
- Set
HttpService.HttpEnabled
totrue
- Download the latest source from this gist
- Create a new ModuleScript with name "Create"
- Set the ModuleScript's contents to the previously downloaded source
- Parent ModuleScript to
ReplicatedStorage
- Set
HttpService.HttpEnabled
back to its original state
Create<T>(className: string)(props: { [any]: any }) -> T
- ExampleCreate.Hydrate<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
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,
}
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" },
}
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 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.
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,
}
Hydrate is a function which allows 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 Hydrate, Event = Create.Hydrate, Create.Event
local blur = Create "BlurEffect" {
Size = 0,
Parent = Lighting,
}
Hydrate(GuiService) {
[Event "MenuOpened"] = function()
blur.Size = 24
end,
[Event "MenuClosed"] = function()
blur.Size = 0
end,
}
The unstable version is not designed for production code. Use at your own risk.
local hs=game:GetService"HttpService";local sl=game:GetService"Selection";local he=hs.HttpEnabled;hs.HttpEnabled=true;local src=hs:GetAsync"https://gist.github.com/cxmeel/2fd7092ed359fc769abc56a38ec74441/raw/Create.Unstable.lua";local m=Instance.new"ModuleScript";m.Name="Create";m.Source=src;m.Parent=sl:Get()[1] or game:GetService"ReplicatedStorage";hs.HttpEnabled=he;sl:Set({m})
- More strongly typed
- Implements
EventGroup
andChildren
SpecialKeys - Uses
Instance:AddTag
instead ofCollectionService:AddTag
to assign tags
The Children
SpecialKey is an alternative to storing child Instances inside the root table. This is purely for aesthetics and is functionally the same as using the root table. The difference between using the Children
SpecialKey and the root table is that the Children
SpecialKey does not care how you index child Instances; however, the index does not have any affect on the creation or properties of the Instance.
local Children = Create.Children
Create "ScreenGui" {
Name = "HelloWorld",
[Children] = {
SomeLabel = Create "TextLabel" {
Text = "Hello, world!",
},
},
}
EventGroups are a way of connecting multiple signals to a single function. EventGroups support the following signals (in the form of SpecialKeys):
- Property changed events
- Attribute changed events
- Instance events (e.g.
.ChildAdded
,.Destroying
) - Constructor SpecialKeys
local EventGroup = Create.EventGroup
local Changed = Create.Changed
local Event = Create.Event
Create "Part" {
[EventGroup { Changed "Position", Event "Touched" }] = function(rbx: Part)
-- called when Part.Position is changed
-- or when Part.Touched
end,
}
Bear in mind that EventGroups do not distinguish between signals, i.e. using the example above, you would not be able to tell within the callback whether it was the position change event or the touch event that triggered the callback. If you need access to the callback parameters from these events, you should not group them together.