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

Is there a way to create your own method/function etc?

Asked by 4 years ago

I was just wondering if it was possible to actually make your own method/function. For example: I could make my own :GetProperties() thing. I just wanted to know what I should learn to be able to do this. And whether it's actually possible. Thanks in advance

0
Would there be any specific things id need to learn before pursuing this JeffTheEpicRobloxian 258 — 4y
0
AFAIK, there's no way. JesseSong 3916 — 4y
0
Alr then JeffTheEpicRobloxian 258 — 4y
0
Actually its possible, I don't recommend doing, but its possible Leamir 3138 — 4y
0
Check object oriented programming and metatables Natsudragneel2500 80 — 4y

1 answer

Log in to vote
1
Answered by
nievadev 112
4 years ago

You can create your own metatables emulating this behavior.

Create a ModuleScript, in it write:

local Module = {}

function Module.new(age)
    -- Age is an example parameter
    -- This new function will return a Module object
    -- And this function works as a constructor for this Module object
    local self = setmetatable({}, Module)

    self._age = age

    return self
end

function Module:GetAge()
    return self._age
end

return Module

:GetAge() method returns the current value of the _age property of the initialized Module object. As you can see, it uses a colon, which is a shorthand for .GetAge(self) (note the period instead of a colon). It's basically syntactic sugar.

Remember to accept the answer if is it helpful for you! I'm glad to help :)

0
Thanks! JeffTheEpicRobloxian 258 — 4y
Ad

Answer this question