Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Adding :Function() to an instance?

Asked by
Mokiros 135
9 years ago

I want to add a function to an instance, something like Part:Clone(). So how do i do it? Here what i want it to be:

function Object:DoSomething()
    --Do something to part
end
0
You cannot add a function directly to an Instance. You can do that in a ModuleScript, however. Marios2 360 — 9y

2 answers

Log in to vote
10
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago
Edited 7 years ago

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:

obj:func( x, y ) -- EDIT: fixed typo

is the same thing as

obj.func( obj, x, y )

That means I can do stupid things like this:

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.

local object = {}

object.func = function(stuff)
    print("Hi", stuff, "!")
end

object.func("me")
-- Hi me !
object:func()
-- Hi table: 0x12345

As another convenient sugar, Lua let's you define functions on objects without using =:

function object.func(stuff)
    print("Hello", stuff, "!")
end

(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!

function object:func()
    print("Hello", self, "!")
end

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

object.func("BlueTaslem") -- EDIT: fixed typo
-- Hello 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:

function MakeBlue(self)
    self.part.BrickColor = BrickColor.Blue()
end

function MakeRed(self)
    self.part.BrickColor = BrickColor.Red()
end

function Wrap(part)
    assert(part:IsA("BasePart"))

    return {
        part = part,
        -- methods
        MakeBlue = MakeBlue,
        MakeRed = MakeRed,
    }
end

-- example
local w = Wrap(workspace.Part)
while true do
    w:MakeBlue()
    wait(5)
    w:MakeRed()
    wait(5)
end
0
This should be the accepted answer. LetThereBeCode 360 — 9y
0
(typos are fixed now -- should no longer be misleading) BlueTaslem 18071 — 9y
0
can you use default methods on this though? creeperhunter76 554 — 7y
Ad
Log in to vote
0
Answered by 9 years ago

What you can do is make a table that holds the object that you want to run function onto. Then, add a method to the table.

local Object = {obj = some_object_variable} -- change some_object_variable to a variable that holds your object

function Object:Transp(num) -- Here is an example function that changes the object
    self['obj'].Transparency = num
end

Object:Transp(0.5) -- Magick! :o

When you assign a method to a table, and it is called via colon (:), something happens in the background that you can't see. An argument is passed, self which holds the table variable that the method is called on.

Hope this helped.

0
lol this is sad iuclds 720 — 4y

Answer this question