Need some help with module scripts and dictionaries!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear! I want to have a dictionary that has connections to functions that are inside a module script.
  2. What is the issue? Include screenshots / videos if possible!!

DevHelp4

this is the server script(no need to worry about the remote event or local script):

local RS = game:GetService("ReplicatedStorage")

local RemoteE = RS:WaitForChild("RemoteEvent")
local M = require(script.M)

local Dic = {
	["Yolo"] = M.Yolo(),
	["olo"] = M.olo(),
	["lol"] = M.lol()
}

RemoteE.OnServerEvent:Connect(function(plr, StringVal)
	Dic[StringVal]()
end)

Note: The values given to Remote onServerEvent is all true(meaning its either “Yolo”, “olo”, “lol”).
And this is the module script(just some basic script to show the problem)

local module = {}

module.Yolo = function()
	print("Yolooooo!")
end

module.olo = function()
	print("oloooow!")
end

module.lol = function()
	print("lolololol!")
end

return module

Note: I know doing it

local Dic = {
	["Yolo"] = function()
		print("Yolooooo!")
	end,
	["olo"] = function()
		print("oloooow!")
	end,
	["lol"] = function()
		print("lolololol!")
	end,
}

this way would work but this would make it so function would take a lot of area on my scripts,
so if there is a way for the way i asked to work i would be happy, thank you for looking at my post.

If I understand you correctly, you want to store some functions of your ModuleScripts in a Dictionary, and later call them from the dictionary, right?
This can be easily achieved by not calling them inside the Dictionary like so:

local Dic = {
	["Yolo"] = M.Yolo,
	["olo"] = M.olo,
	["lol"] = M.lol
}

Later, you can easily call them like so:

print(Dic.Yolo())

Or in your case,

Dic[StringVal]()

Adding parenthesis after a function calls it. If you want to store the function in a variable itself rather than storing its return value, just don’t call it. I’m not the best at explaining but I hope this isn’t that difficult to understand. :slightly_smiling_face:

It worked you have my gratitude, thank you.

1 Like