Introduction
Hey fellow developers! I was actually thinking of a solution (for beginners) to a common problem with events.
The Problem
The biggest issue is the performance loss that comes with having multiple connections. Also the use of PlayerAdded, CharacterAdded and Died events are usually improper as well.
The Solution
I’ve shown a solution to many of these problems. By disconnecting events, adding a reference, and reconnecting the humanoid death event every time the character is added
The Code
local Players = game:GetService("Players") -- This is good practice, you should use game:GetService("Players") instead of game.Players or whatever other service you want to get.
-- Put the events in tables! It prevents memory leaks!
local CharacterAddedConnections = {}
local DiedConnections = {}
local function playerAdded(player)
print("New player:", player.Name)
local function characterAdded(character)
print(player.Name, "respawned")
local function onDied()
print(player.Name, "died")
DiedConnections[player]:Disconnect() -- disconnect when event is ended! always put at the end
end
DiedConnections[player] = character:FindFirstChildOfClass("Humanoid").Died:Connect(onDied) -- add a new one! the previous one was most likely disconnected (if this isn't the first time they joined)
end
CharacterAddedConnections[player] = player.CharacterAdded:Connect(characterAdded) -- add this one to a table! disconnect when they leave!
end
local function playerRemoving(player)
print("Player left:", player.Name)
CharacterAddedConnections[player]:Disconnect() -- they left, so there's no need to keep it connected.
if DiedConnections[player] then
DiedConnections[player]:Disconnect()
end
end
-- there's no reason in adding a connection to disconnect here. it when the game closes, you should always keep these conncted.
Players.PlayerAdded:Connect(playerAdded)
Players.PlayerRemoving:Connect(playerRemoving)
for _, player in pairs(Players:GetPlayers()) do -- always do this! if you add wait statements then your playeradded event wont fire. it's just good practice.
playerAdded(player)
end
Incorrect Usage
game.Players.PlayerAdded:Connect(function(Player) -- why is it a new function? there should be an existing function rather than creating a new one in the function argument
Player.CharacterAdded:Connect(function(Player)
end)
-- don't do this
Player.Character.Humanoid.Died:Connect(function() -- also, this is going to connect a bunch. please don't do this.
print("blah blah blah")
end)
-- put died in character added, not here.
end)
-- where's the for loop?
-- where's the connection? why connect up there ^?
Conclusion
-
Create references to events in tables that may create multiple connections (Ex: Events within Events)
- Disconnect events when they aren’t needed (When the player leaves, when he respawns, etc)
- Also do this when new connections may add up, causing performance issues.
- Disconnect events when they aren’t needed (When the player leaves, when he respawns, etc)
-
Reconnect Died in CharacterAdded (Make sure to use a new reference to the humanoid, not pre-existing ones!)
Feel free to ask me any questions.