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

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

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

✓ Accepted answer

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!

2 comments

0

Thankyou So Much:D My Script worked out

0

thank YOU i only knew that you could do anonymous functions, not actual functions. hope your projects goes well

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