TL;DR: No. See below for what :
means and why you can't add extra methods.
Sugars!
A function like part:Clone()
is called a method.
Methods are just sugar for function calls:
is the same thing as
That means I can do stupid things like this:
1 | workspace.Destroy( workspace.Part ) |
which will, in fact, destroy workspace.Part
.
How does a function get attached to an object? The same way any other value does.
03 | object.func = function (stuff) |
04 | print ( "Hi" , stuff, "!" ) |
As another convenient sugar, Lua let's you define functions on objects without using =
:
1 | function object.func(stuff) |
2 | print ( "Hello" , stuff, "!" ) |
(Note that you can't use []
in function definitions like this, for some reason)
If you have a function that you generally expect to be a method, it would be nice to just write it out, instead of having to always write out the first parameter and make it look like a normal function.
Lua provides more sugar for that!
2 | print ( "Hello" , self, "!" ) |
This implicitly defines self
as the first parameter of the function. It's exactly the same as the previous definition, though, so I can still say
1 | object.func( "BlueTaslem" ) |
Adding things to Instances
ROBLOX objects don't let you add new methods to them -- this is a result of not being able to add new properties to objects.
The good news about this is that when you invoke a method on a ROBLOX object, every script will agree on how to do it -- because ROBLOX says how to do it, not some script buried in your place that defined a different behavior.
If you want to make methods, you have to have your own objects!
You can "wrap" ROBLOX objects in order to extend their functionality -- create your object that exposes all of the things you want, and implement some of those in terms of an instance that you have:
01 | function MakeBlue(self) |
02 | self.part.BrickColor = BrickColor.Blue() |
06 | self.part.BrickColor = BrickColor.Red() |
10 | assert (part:IsA( "BasePart" )) |
21 | local w = Wrap(workspace.Part) |