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

Any Way To Refer To The Functions or Property by changing the propertyname to string?

Asked by
Yuuwa0519 197
4 years ago

I know the title is a bit confusing, but I couldn`t think of any good name. Anyways, what I want to ask is, is there any way to refer to properties or function by string? For example, I have a module script that contains this:

local module = {}
function module.blah1(bool)
    if bool == true then
        print("Blah1")
    end
end

function module.blah2(bool)
    if bool == true then
        print("Blah2")
    end
end
return module

And the normal script will require this when Player Fires RemoteEvent.

local blahModule = require(script.ModuleScript)

local function printBlah(plr,actionName)
    --This Part Will Be Explained Later.
end)

game.ReplicatedStorage.BlahRemote.OnServerEvent:Connect(printBlah)

Now, If I were to make actionName either "Blah1" or "Blah2", Is there anyway to run the module function something like this?

local function printBlah(plr,actionName)
    blahModule.actionName(true)
end)

Sorry for confusing text, and thankyou for any helps :D

1 answer

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

yeah, you do both anonymous functions and regular functions like so:

MAIN SCRIPT


module = require(game.Workspace.ModuleScript) module["testFunction"]() module["testAnonFunction"]()

MODULE SCRIPT

local module = {}

--anonymous function inside of a variable
module.testAnonFunction = function()    
    print("test anon function worked!") 
end

--regular function inside of module table 
function module.testFunction()
    print("test function worked")
end

return module

OUTPUT:

test function worked

test anon function worked!

0
Thankyou So Much:D My Script worked out Yuuwa0519 197 — 4y
0
thank YOU i only knew that you could do anonymous functions, not actual functions. hope your projects goes well royaltoe 5144 — 4y
Ad

Answer this question