Studio kept crashing for me whenever I would test my game and then end the test. I couldn’t figure out why for the longest time but it turns out it traces back to the aforementioned event. It’s connected to a function that checks to see if the object being removed was important (e.g. some exploiter deleted important game items) and replaces it. When you end a test in studio the game deletes all of the objects and I’m assuming it loops through until everything is deleted. When the test ends and the game deletes the objects my script tries to replace them and it ends up freezing and crashing.
This is fairly avoidable I just thought I would warn about it.
Note that this will probably also cause servers for your game to hang when shutting down, this won’t result in data loss as the OnClose callback is called first but it still isn’t ideal.
The reason for this is that you cause an infinite loop by adding items to the DataModel while the engine is trying to remove them one at a time, for obvious reasons it can never complete this operation. I think this could be fixed on the C++ side by tearing down the DataModel in a different way but I’m not sure if this is a problem that needs solving.
As I mentioned above OnClose runs first so you can solve your problem like so:
local addPart = true
game.OnClose = function()
addPart = false
end
game.Workspace.ChildRemoved:connect(function(removedChild)
if addPart then
Instance.new("Part", game.Workspace)
end
end)
Surely when the game is about to shut down (i.e. OnClose has finished and there is no way to prevent the server from shutting down anymore), events don’t have to be fired anymore?
I guess not. As this thread shows these events can be fired while the data model is being destroyed, however you can’t yield at all in the called function so it seems useless. So you’d never get ChildRemoved or PlayerRemoving events when the game shut down but it would make no difference as far as I can tell.