Is there any way to make a function that uses : on an instance?

Is there any way you can create a function that works on instances like FindFirstChildOfClass() ? Something like:

function Recolor() -- or Instance:Recolor()
instance.BrickColor = BrickColor.new("Lime green")
end

Part:Recolor()

This is just a small question, i know you can do something like Recolor(Part) but i’d like to know if you can use the colon with it. I’m sorry if this is an inconvenience.

1 Like

You can’t really add your own methods to Roblox instances but you can create your own classes using module scripts and create any method for those that you choose.

Put this into a module script

local customPart = {}
customPart.__index = customPart

--Function to create a new instance of your custom part
function customPart.new(partInstance)
	
	local new = {}
	setmetatable(new, customPart)
	
	new.Part = partInstance
	
	return new
end

function customPart:ReColor(color)
	self.Part.BrickColor = color
end


return customPart

–Usage

local CustomPart = require(myCustomPartModuleScript)
local myCustomPart = CustomPart.new(part)
myCustomPart:ReColor(BrickColor.Random())
3 Likes

Thank you very much for answering, but i do have a few questions to ask. Firstly, what is _index? and what is the need for a metatable (i have no idea what they are) and why the need for two seperate functions?

_index basically tells lua to search the customPart table when using operators like : or . to find properties or methods. The setmetatable is telling lua to do basically the same thing for the new table/instance you create.

The first function “.new” creates an instance of the “customPart” table that has its own unique property values. So any new instance would not share property variables.

You probably don’t need to deal with any of these at all. It makes things really complex especially if you’re not that familiar with Lua. But if you’re interested:

What you’ve just shown is much more complicated version of the example I gave him… My example is a simple wrapper class for a part.

You’ll have to end up writing more code if you want to have it behave smoothly like an actual Instance.

Why must you create a new instance? Can you not apply it to normal instances?

You’re just creating a new instance of the custom part class. Each instance of the custom part class would have its own unique “part” property. Without doing it that way you’d have to create a whole bunch of different variables to hold different parts or the part property would be overwritten every time you worked with a different part.

With creating a new instance:

local CustomPartClass = require(myCustomPartModuleScript)
local myCustomPart1 = CustomPartClass.new(part1) --Wrap part1 with the class
local myCustomPart2 = CustomPartClass.new(part2) --Wrap part2 with the class

myCustomPart1:ReColor(BrickColor.Blue()) --Part1 is now blue
myCustomPart2:ReColor(BrickColor.Red()) --Part2 is now red

Without the new method in the class:

local customPart = {}
customPart.__index = customPart

customPart.Part = part1

function customPart:ReColor(color)
	self.Part.BrickColor = color
end

return customPart

–Usage

CustomPartClass:ReColor(BrickColor.Blue()) --part1 is blue

CustomPartClass.Part = part2 --change the part to work with
CustomPartClass:ReColor(BrickColor.Red()) --part2 is red

Or you’d have to pass the part to work with into the method each time:

local customPart = {}
customPart.__index = customPart

function customPart:ReColor(part, color)
	part.BrickColor = color
end

return customPart

–Usage

CustomPartClass:ReColor(part1, BrickColor.Blue()) --part1 is blue
CustomPartClass:ReColor(part2, BrickColor.Red()) --part2 is red