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

Use method by its string name?

Hello, Community. I have a question that concerns something I tried to do in Roblox Lua and ran into an error.

Question: How could I go about calling a method by it's string name?

For example:

-- In a Local Script.

local mname = "GetService"

if game[mname]("Workspace") ~= nil then
	print("Workspace isn't nil.")
end

This outputs an error saying: "Expected ':' not '.' calling member function GetService."

I've tried a lot of other things I could think of to achieve this, but no success so far.

I know that it should be something like: if game:GetService("Workspace") ~= nil then print("Workspace isn't nil.") end

But how could I call the GetService method with it's string name? Thanks in advance.

9 comments

0

ok but why

1

game("GetService", "Workspace") __namecall

1

also game["GetService"] is equivalent to game.GetService, and game:GetService("Workspace") is sugar for game.GetService(game, "Workspace")

0

[""] is the alternative method for index something with a StringName that interferes with the Scripts syntaxing, it is the same as .

0

GetService requires a Colon

0

No. It does not. As I just said, game:GetService("Workspace") is sugar for game.GetService(game, "Workspace"), if you have some experience in OOP you'd know that.

0

The way OP is calling GetService is not with the ":" notation.

0

Thanks for the help, yeah, actually I was trying to overwrite the __namecall metamethod for a sandbox environment where everything is wrapped using proxies.

0

Well, not overwrite since the metatable is blank.

1 answer

✓ Accepted answer

Basically just a writeup of what incapaxian said

The : operator is just "syntax sugar" for "." but with the first argument being the object you're calling from. "Syntax sugar" is a term for something which is syntactically equivalent to something else, but that is easier and cleaner to use. If you ever use Haskell, get ready for a lot of syntax sugar, because the language is basically built on it.

For example, these are equivalent:

game:GetService("Players")
game.GetService(game, "Players")

Furthermore, in Lua, the . operator is just sugar for an array access. The following are equivalent

table["banana"]
table.banana

Every different data structure in Lua is just a table wearing a different hat, so these rules apply to basically any data structure, including objects.

Therefore, this should do what you want:

function methodCall(owner, methodName, arguments)
  owner[methodName](owner, unpack(arguments))
end

methodCall(game, "GetService", {"Players"})

1 comment

0

Not sugar for an array access, array indices are integer and table.1 would not work. Also, for the methodCall function you can just make the function take a tuple of values

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