I’m trying to optimize my game as much as possible, especially since fps drops to 52 when players’ respawns (why?) and I figured connections might a play role but I’m not sure to handle them properly.
local Players = game:GetService("Players")
local Connections = {}
Players.PlayerAdded:Connect(function(Player)
Connections[Player.Name] = {}
Player.CharacterAdded:Connect(function(Character) -- On PlayerRemoving do I also disconnect this?
-- Connections relating to player
table.insert(Connections[Player.Name], Character.Humanoid.Died:Connect(function()
for _,v in Connections[Player.Name] do
v:Disconnect()
end
table.clear(Connections[Player.Name])
end))
end)
end)
Or
local Players = game:GetService("Players")
local Connections = {}
Players.PlayerAdded:Connect(function(Player)
Connections[Player.Name] = {}
Player.CharacterAdded:Connect(function(Character)
-- Connections relating to player
table.insert(Connections[Player.Name], Character.Humanoid.Died:Connect(function()
--
end))
end)
Player.CharacterRemoving:Connect(function(Character) -- usually occurs on death, recommended to use character removing apparently?
for _,v in Connections[Player.Name] do
v:Disconnect()
end
Character:Destroy() -- Necessary or not?
table.clear(Connections[Player.Name])
end)
end)
Players.PlayerRemoving:Connect(function(Player)
for _,v:RBXScriptConnection? in Connections[Player.Name] do
pcall(function()
v:Disconnect()
end)
end
table.clear(Connections[Player.Name])
Connections[Player.Name] = nil
end)