I’m wanting to get thoughts/opinions on this setup for my gamemodes module. Basically, each gamemode has their own module, a Start and Cleanup function.
Start basically starts and controls any actions that are done in a game. In this example, Team Deathmatch checks for any deaths, and adds points to specific team when player dies.
I just wanna know if this is efficient, is gonna lead to any memory leaks or anything, etc.
local TDM = {}
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local ScoreManager = require(script.Parent.Parent.ScoreManager)
local Shared = ReplicatedStorage:WaitForChild("Shared")
local Maid = require(Shared.Maid)
local Blue = Teams:WaitForChild("Blue")
local Red = Teams:WaitForChild("Red")
local Connections = {}
local Debounces = {}
--// Start the game
function TDM.Start()
for _, player in pairs(Players:GetPlayers()) do
local Character = player.Character
if not Character then continue end
Connections[#Connections + 1] = Character.Humanoid.Died:Connect(function(character)
if Debounces[player.UserId] then return end
Debounces[player.UserId] = true
local Creator = Character:FindFirstChild("Creator")
if not Creator then return end -- No killer
if player.Team == Blue then
ScoreManager.Add(Red)
elseif player.Team == Red then
ScoreManager.Add(Blue)
end
end)
Connections[#Connections + 1] = player.CharacterAdded:Connect(function(character)
if not Debounces[player.UserId] then return end
Debounces[player.UserId] = nil
end)
end
end
--// Do any cleaning up
function TDM.Cleanup()
for i = 1, #Connections do
Connections[i]:Disconnect()
Connections[i] = nil
end
Debounces = {}
end
return TDM