Recently, my game is experiencing memory leak causing the server to crash with error 277.
I’m not really that clear with how memory leaks work, can anyone let me know whether the follow script involves memory leak?
script.Parent.Acceptance.Touched:Connect(function(hit)
if hit.Name == "TouchSensor" then
script.Parent.Settings.Train.Value = hit.Parent.Parent.Parent
script.Parent.Settings.StopCorrect.Value = true
local touchconnection; touchconnection = script.Parent.Acceptance.TouchEnded:Connect(function(hit)
if hit.Name == "TouchSensor" and hit.CollisionGroup == "Trains" then
script.Parent.Settings.StopCorrect.Value = false
if script.Parent.Settings.Train.Value ~= nil then
script.Parent.Settings.Train.Value.Values.PSD.Value = ""
script.Parent.Settings.Train.Value = nil
end
touchconnection:Disconnect()
end
end)
end
end)
script.Parent.Settings.Train.Changed:Connect(function(val)
if val ~= nil then
val.Values.PSD.Value = string.sub(script.Parent.Name,5,-1)
OpenDoorEvent = val.Values.DoorOpen.Changed:Connect(function(status)
if val.Values.DoorSide.Value == script.Parent.Settings.DoorSide.Value then
if status == true then
script.Parent.DoorOpen.Value = true
else
wait(.9)
script.Parent.DoorOpen.Value = false
end
end
end)
else
if OpenDoorEvent then
OpenDoorEvent:Disconnect()
end
print(OpenDoorEvent) --returns Connection
end
end)
Memory leaks occur when something is created but not destroyed.
To find memory leaks, identify every instance in the script where something is created, then determine whether it will be destroyed.
For example, the events you connect. They are disconnected when the instance they are from is destroyed or they are disconnected manually (or through :Once, etc…).
If you create an event, and don’t disconnect it, it’s fine. If you create an event, and the object is discarded, and you don’t disconnect it, it’s okay. But if you create an event, and the object is discarded, and you do it repeatedly, forever, that’s a memory leak, and it’s a problem.
Thanks.
I’m pretty concerned about printing the connection still doesn’t return nil, it looks like it isn’t a problem I assume?
Was pretty worried about the memory leak since my game is facing error 277 rn, was kinda suspecting if this script spam creating connections were the case.
You should be able to look into the console, and check the memory section where you can find all the scripts and see the memory they are taking up. I also believe there is a viewer in studio to see the performance of all scripts, and you might also be able to see the memory they take up there too.