Is it OK to require a module every time a function is called?

I have a module script that handles the different gamemodes that will be present in my game and I was wondering how I could optimize it/write it more efficiently.

To explain why I have it like this, it is simply because it seemed like the shortest and neatest way of writing the code for this specific scenario. However, I am worried what problems and/or performance issues may arise from requiring a module every time a function is called.

I want to find a more efficient and safer way of doing what I’m showcasing below. I also included a screenshot of how the modules are setup in the explorer so you can better understand. Any help would be greatly appreciated!

gamemodes_architecture

--//Services
local rep = game:GetService("ReplicatedStorage")

--//References
local serverData = rep.ServerData
local roundData = serverData.Round

--//Functions
local Gamemodes = {}

function Gamemodes.CheckPlayerRemoved(activePlayers, player)
	local gamemode = roundData.Gamemode.Value
	require(script[gamemode]).CheckPlayerRemoved(activePlayers, player, roundData.Roles)
end

function Gamemodes.GetRoles(activePlayers)
	local gamemode = roundData.Gamemode.Value
	require(script[gamemode]).GetRoles(activePlayers, roundData.Roles)
end

function Gamemodes.GiveWeapons()
	local gamemode = roundData.Gamemode.Value
	require(script[gamemode]).GiveWeapons(roundData.Roles)
end

-- keep in mind this module is not finished but I wanted to make this post before I have a bunch of functions doing this

return Gamemodes

Modules cache when required, so it should be fine. For further information:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.