Let’s be honest, writing out every single property when you create a new instance is annoying and tedious. For example:
local textLabel = Instance.new("TextLabel");
textLabel.Size = UDim2.new(1, 0, 1, 0);
textLabel.Position = UDim2.new(0, 0.5, 0.5, 0);
textLabel.RichText = true;
textLabel.Text = "<b>My Label!</b>";
textLabel.TextColor3 = Color3.fromRGB(50, 50, 50);
textLabel.Name = "MyLabel";
textLabel.Parent = myScreen;
There’s a solution to this. It’s known as method chaining. I’m used to using this pretty frequently in Java and figured I would try to port this concept over for use with instances. So the code above becomes:
local CustomInstances = require(script.CustomInstances);
CustomInstances.new("TextLabel")
.size(UDim2.new(1, 0, 1, 0))
.pos(UDim2.new(0.5, 0, 0.5, 0))
.rich(true)
.text("<b>My Label!</b>")
.textColor(Color3.fromRGB(50, 50, 50))
.name("MyLabel")
.parent(myScreen);
Alternatively you can chain each method on the same line I simply prefer using new lines in this instance to prevent eye sores.
You can also append .get()
to get the Roblox instance itself back:
local part = CustomInstances.new("Part").name("A Part").parent(workspace).get();
A lot of these methods have aliases, however, you can use the property name itself except in camel case if you’re not familiar with the aliases. E.g. let’s say you wanted to set the TextTransparency
property of a text label. Normally you would do label.TextTransparency = 1
. The method alternative of this is label.textTransparency(1)
. Once again notice how the method uses camel case.
I primarily made this for fun and I don’t have the time to implement support for every single Roblox class. If you want to add support for a specific instance simply create a module script under the CustomInstances module and name it the instance name. Some instances such as Parts inherit properties from other classes (e.g. BaseParts). There’s also support for this by prefixing the module with C_
then appending the class name.
Download the modules here:
CustomInstances.rbxm (3.6 KB)
I’ll probably add more classes and reupload every once in a while with the updated modules. Hope you enjoy.