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

How to invoke methods without ':'?

Asked by
Ekkoh 635 Moderation Voter
10 years ago

I have a variable for the name of the method (i.e. local method = "GetChildren"), so I first tried indexing the method by object[method], which works. Problem is, when I try to actually invoke the method, object[method](), I get the following error. I'm wondering if there's a way around this, because it's kind of a big part of what I'm making.

> print(Workspace["GetChildren"])
function: 08553E58
> print(Workspace.GetChildren)
function: 08553E58
> for key, val in pairs(Workspace["GetChildren"]()) do print(key, val) end
19:47:04.245 - Expected ':' not '.' calling member function GetChildren
0
Well, the "[]" means it's a child of the defined path, which GetChildren isn't.. Shawnyg 4330 — 10y

1 answer

Log in to vote
5
Answered by 10 years ago

"Methods" (called by ':') are syntactic sugar for calling a function on an object. These kinds of functions have a 'self' argument. If you want to do something like you are trying to do, you must do the following:

local method = "GetChildren"
object[method](object)
--Alternatively, if you were calling a function with arguments
local method = "FindPartOnRay"
workspace[method](workspace, ray, ignore, bool)
Ad

Answer this question