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

Is there a way to make a function like a method?

Asked by 9 years ago

Say I had:

function PrintName(Input)
    print(Input.Name)
end

And I wanted to use this as a method, like such:

game.Workspace.Part:PrintName()

Would I have to use the usual PrintName(game.Workspace.Part) or would that work?

3 answers

Log in to vote
0
Answered by
MrFlimsy 345 Moderation Voter
9 years ago

It won't work. However, it is possible to create your own methods.

This wiki article should help.

0
Thanks. SquirreIOnToast 309 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Sorry these people aren't really answering your question, I'll do my best attempt to do so.

You could give your parts and objects their own methods, to the point where you could literally write,

game.Workspace.Part:PrintName()

and get a result as expected, but it's honestly not worth it. It can be easily done however through the use of a custom environment. If you want me to, I can write something quick up for you explaining how to do it.

0
Yes please. That would be great. SquirreIOnToast 309 — 9y
Log in to vote
-1
Answered by 9 years ago

I see what you are asking. What you want to do wouldn't be accomplished using new methods, but using parameters.

To call your function, you can use the following:

PrintName(game.Workspace.Part)

For example, if you wanted to use that to print the name of all parts in a model, you could use the following:

for i,v in pairs(game.Workspace.Model:GetChildren()) do
    PrintName(v)
end

That would print the name of every item in the model by calling the function PrintName, setting its parameter to the item's name.

0
That's not what I asked. SquirreIOnToast 309 — 9y
0
It has the same effect. You don't need to rate me down for this. SlickPwner 534 — 9y
0
I didn't do it. Some other noob did. Serves you right though because you didn't help. :P SquirreIOnToast 309 — 9y

Answer this question