Been thinking about this question for a bit recently, since I have adjusted how I do it. Is there a “better” way of doing this, whether is it optimization, cleaner code whatever the case.
Method 1
module.functionName = function()
end
Method 2
function Module.FunctionName()
end
Method 3
— Similar to the first one
module[“FunctionName”] = function()
end
local Module = {
FunctionName = function()
end
}
return Module
in terms of optimization all your three end up looking like this compiled, it’s just defining a function in or outside of the dictionary before its returned to whatever is require()'ing the modulescript
The developer wiki uses method 2 in its examples so it’s likely popular by first impression.
Method 1/3 remind me of Ye Olde times when an event connection might look like: game:GetService("ReplicatedStorage").Event.OnClientEvent = function() [...] end
This can vary a lot, depending on what you are using it for.
What I usually do to keep everything clean is:
CommandsManager.Messages = {
NoPermission = 'You do not have access to execute this command! Required rank is %s.',
NotHighEnoughRank = 'You do not have high enough rank to do that!',
Error = 'Unable to execute command! Contact a developer for more information.',
NoPlayerFound = '%s does not exist!',
WrongUsage = 'Wrong usage! Use %s.',
TooManyRequests = 'Slow down! Too many requests are being made against this key.',
AlreadyBanned = '%s is already banned!',
}
Another example for functions:
CommandsManager.Functions = {
Alert = function(Player, String)
local Speaker = ChatService:GetSpeaker(Player.Name)
if (Speaker)
then
Speaker:SendSystemMessage(String, CommandsManager.Settings.Channel)
end
end
}