This dilem is complicated to solve!

Hello everyone today I face a very big dilem complicate to solve

I have a piece of code, which normally should go in three modules (for a mini game system) basically the code has three functions: (loader, setMiniGame and destroyMiniGame)
except that if I put these three functions on each module (MiniGame1, MiniGame2 and MiniGame3) I am afraid that there is an optimization problem so I thought to put it in ManageMiniGame, but I don’t know if this is a good idea if you could ever help me out her would be really cool!

image

TECH 1:

--Put in ManageMiniGame
--//Service
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local manageMiniGame = ReplicatedStorage.Modules.ManageMiniGame
local Red = require(ReplicatedStorage.Libs.Red)
local miniGames = script.MiniGames:GetChildren()
local remoteEvent = Red.Server("Events")
--//Object
local object = ReplicatedStorage.Object
local arsenal = object.Arsenal
local miniGamesMap = object.MiniGamesMap
--//Array
local miniGamesInfo = {
	["MiniGame1"] = {Name = "Museum", Description = "This is Museum", Time = 10, Color = Color3.new(255, 0, 0)} :: miniGameInformation,
	["MiniGame2"] = {Name = "Desert", Description = "This is Desert", Time = 10, Color = Color3.new(0, 255, 0)} :: miniGameInformation,
	["MiniGame3"] = {Name = "Arena", Description = "This is Arena", Time = 10, Color = Color3.new(89, 34, 89)} :: miniGameInformation
}
--//Typage
type miniGameInformation = {Name: string, Description: string, Time: number, Color: Color3, Map: Model}
type playerInfo = {Name: string, DisplayName: string, UserId: number, Player: Player}
--//function

local function printTextChatBox(message: string)
	remoteEvent:FireAll("ChatBoxEvent", message)
end

local function destroyMiniGame(miniGame: {})
	for _, character in workspace:GetChildren() do
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			local arsenal = player.Backpack:FindFirstChildWhichIsA("Tool") or character:FindFirstChildWhichIsA("Tool")
			character.HumanoidRootPart.CFrame = workspace.SpawnLocation.CFrame * CFrame.new(0, 2, 0)
			arsenal:Destroy()
		end
	end
	workspace.MiniGame:FindFirstChild(miniGame.Name):Destroy()
	printTextChatBox(`<font color=\"rgb({miniGame.Color})\">{miniGame.Name} is Finished !</font> `)
	local ok, result = pcall(require, manageMiniGame)

	if ok and result.Callback and result.Name then
		return result.Callback()
	else
		warn(result.Name .. " can't load:", result)
	end
end

local function setMiniGame(miniGame: {}, allPlayers: {})
	local cloneMap = miniGamesMap:FindFirstChild(miniGame.Name):Clone()
	local cloneArsenal = arsenal[miniGame.Name]:FindFirstChildWhichIsA("Tool"):Clone()
	cloneMap.Parent = workspace.MiniGame
	for _, character in ipairs(workspace:GetChildren()) do
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			cloneArsenal.Parent = allPlayers[player.Name].Player.Backpack
		end
	end
	task.wait(miniGame.Time)
	destroyMiniGame(miniGame)
end

local function getMiniGame()
	local getMiniGames = miniGames[math.random(1, #miniGames)]
	local allPlayers = {} :: {[string]: playerInfo}
	
	local ok, result = pcall(require, getMiniGames)
	local miniGameArray = miniGamesInfo[result.Name]
	
	if ok and result.Callback and miniGameArray then
		for _, character in ipairs(workspace:GetChildren()) do
			local player = Players:GetPlayerFromCharacter(character)
			if player then
				allPlayers[player.Name] = {
					Name = player.Name,
					DisplayName = player.DisplayName,
					UserId = player.UserId,
					Player = player
				}
				character.HumanoidRootPart.CFrame = workspace.Part.CFrame
			end
		end
		result.Callback()
		setMiniGame(miniGameArray, allPlayers)
	else
		warn(result.Name .. " can't load:", result)
	end
end

local function lobbyWaitMiniGame()
	task.wait(10)
	getMiniGame()
end

return {
	Callback = lobbyWaitMiniGame,
	Name = "PlayerLoading"
}

or
TECH 2

--Put in MiniGame1, MiniGame2 and MiniGame3
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Red = require(ReplicatedStorage.Libs.Red)

local remoteEvent = Red.Server("Events")
local manageMiniGames = ReplicatedStorage.Modules.ManageMiniGame

local function printTextChatBox(message: string)
    remoteEvent:FireAll("ChatBoxEvent", message)
end

local function loadMiniGames(miniGame: {}, allPlayers: {})
    local cloneMap = miniGame.Map:Clone()

    cloneMap.Parent = workspace.MiniGame
    
    task.wait(miniGame.Time)
    for _, character in workspace:GetChildren() do
        if Players:GetPlayerFromCharacter(character) then
            character.HumanoidRootPart.CFrame = workspace.SpawnLocation.CFrame * CFrame.new(0, 2, 0)
        end
    end
    cloneMap.Parent = nil
    local ok, result = pcall(require, manageMiniGames)
    
    if ok and result.Callback and result.Name then
        printTextChatBox(`<font color=\"rgb({miniGame.Color})\">{miniGame.Name} is Finished !</font> `)
        return result.Callback()
    else
        warn(result.Name .. " can't load:", result)
    end
end

return {
    Callback = loadMiniGames,
    Name = "MiniGame1"
}

what TECH its better ?

(From Lua Performance Tips written by the author of Lua)


There shouldn’t be an optimization problem with using module scripts. Even if there was some decent performance gain, I wouldn’t recommend sacrificing readability for performance in that manner.

3 Likes

The question is, before thinking this is an issue, have you tried your game? Or have you tested its performance at scale if you will? Optimising something which you don’t really know if it needs optimisation is really something you shouldn’t do unless you need to, 'cuz if its working, don’t touch it!