I was reading some post about :Connect on Roblox Developer and this part got my attention
Always call Disconnect() when a connection is no longer needed. Forgetting to do so can cause your game to use more resources than necessary. Note, however, that this may not always be necessary; when an object is destroyed, all connections to that object’s events are disconnected automatically.
And then i wondered how can this affect performance? A lot? Is it even notorious for the player? I use :Connect a lot, and when i say a lot believe me. It’s a LOT and i’m concerned if this can make my game laggy/less responsive. nd if it really causes performance issues is there any alternative? (other than while true do)
I never heard of :Connect().
I mean, I guess you mean
local t
t = Event.OnServerEvent:Connect(function()
t:Disconnect()
end)
About performance.
I mean…
Depends.
A few will not take a LOT of memory.
But, talking about MANY events, than yes.
OBviously it’ll be notorious for the player, for example, on firing a event, on getting data from server, the response will be prob lately.
Connect() only takes memory, nothing else. It’s what you do with he connections that affect performance, connection themselves only take up memory(not a lot).
But it just good practice to disconnect functions, because sometimes not disconnecting them can produce other errors, like accidentally duplicating things.
while true do isn’t a alternative to this though, between those:
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local currentHealth = humanoid.Health
while true do
if humanoid.Health ~= currentHealth then
print("HealthChanged")
currentHealth = humanoid.Health
end
wait()
end
You should consider using the second option over the first since it consumes less memory.
What that post meant is that having lots of connections and not disconnecting them when you dont need them anymore will be bad for memory.
Like this:
local willCheck = true
workspace.ChildAdded:Connect(function(child)
if willCheck then
if child.Name == "a" then
willCheck = false
end
end
end)
Doing this would be bad for performance because it will keep on listening to the event of child added, all that happened there was it wont go through the check but it will be bad because it will keep the connection in the memory.
Therefore you should go for this when you dont need it anymore:
local connection
connection = workspace.ChildAdded:Connect(function(child)
if child.Name == "a" then
connection:Disconnect()
end
end)