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

Is requiring lots of different modules more efficient than a huge collection of if statements?

So I have my original admin commands that I made approx. 20 months ago and they work fine, but, they are incredibly laggy due to the immense load of if statements, checking if it's this command or that command. At the moment I am currently switching to one small script requiring separate modules to execute said commands, here is an example of the server script to module script code:

-- // Server
game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
         require(script[msg:split(" ")[1]]):Execute(plr, msg)
    end)
end)

(

-- // Module
return{
Execute = function(s, plr, msg)
    -- (code)
end;
}

Is this fine? Could I do something more efficient? Thanks for your help.

1 answer

✓ Accepted answer

You shouldnt have to use if statements to differentiate between the commands

Example module

local commands = {}

function commands.kick(plr , msg)
	
end

function commands.ban(plr, msg)

end

function commands.whatever(...)

end

-- morecommands

return commands

script

-- // Server
local commands = require(commands)
game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
       		commands[msg:split(" ")[1]](plr, msg)
    end)
end)

if you have a module for each command or module that contains all the commands make no difference at all

1 comment

0

I see

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