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
ok but why
game("GetService", "Workspace") __namecall
also game["GetService"] is equivalent to game.GetService, and game:GetService("Workspace") is sugar for game.GetService(game, "Workspace")
[""] is the alternative method for index something with a StringName that interferes with the Scripts syntaxing, it is the same as .
GetService requires a Colon
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.
The way OP is calling GetService is not with the ":" notation.
Thanks for the help, yeah, actually I was trying to overwrite the __namecall metamethod for a sandbox environment where everything is wrapped using proxies.
Well, not overwrite since the metatable is blank.