Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to invoke methods without ':'?

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

1 comment

0

Well, the "[]" means it's a child of the defined path, which GetChildren isn't..

1 answer

✓ Accepted answer

"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)

This discussion is preserved as it was. New answers, comments, and votes are closed.