Does the PlayerRemoving event not fire when disonnected?

Hey developers!
For my game I keep getting tens of people reporting their data didn’t get saved. After a while it became clear that it only happens when players get disconnected, so either because of internet problems or because they were AFK for too long.


The way my game saves data is a quite fail-proof system after .PlayerRemoving, so it seems as Roblox does not fire a .PlayerRemoving event when players get disconnected, and then when they connect again they still start as new players.
Does anyone know if this is true? Any insights would be greatly appreciated, thank you in advance.

2 Likes

The reason is that PlayerRemoving fires, but for a short amount of time when the last player leaves, the server is forced to shut down, or it’s a network problem
Due to this you should use game:BindToClose()

Check this tutorial out:

4 Likes

Yes, player removing don’t fire in cases when leaving isnt initiated by player, so it’s not save to use it for saving data, if you want to prevent further data loss I’d recomend to use datastore2.

1 Like

Thanks for your reply. I am using BindToClose already, sorry that I forgot to mention it. But so that is not really the problem here, and it also happens when the server still has plenty of players.

Well then I’m not so sure since I’ve never seen this. I’ll try to figure something out. Can you send your script?

Ohh okay thank you. That kind of sucks but it also is the explanation to why this bug was happening then.
Do you know if there is any other event fired when a player gets disconnected? Then I can just save their data when that event gets fired.
And I’ll check datastore2 out, thank you for recommending it.

You can, rather then trying to find an event that fires when player leaves, make thread where you create data for player (mostl ikely connected to PlayerAdded event) yield untill player not exists in game. Something like that:

game.Players.PlayerAdded:Connect(function(ppl)
--your data set here
repeat wait() untill not ppl
--your data saving
end

Of course in this case data need to be saved over time except for saving it when player leaves, since something unexcepted may happen not just with player but with roblox servers.

1 Like

That’s not true

local Players = game:GetService("Players")

game:BindToClose(function()
	task.wait(4)
end)

game.Players.PlayerAdded:Wait()

task.wait(3)

print("Kicking...")

Players:GetPlayers()[1]:Kick()

Players.PlayerRemoving:Wait()

print("Player removing fired!")

playerremoving always fires 2

cc @santokioS

1 Like

Oh okay that’s a relief, thanks for the reply.
But do you know if this also work with the “lost connection” pop-up, where they still have the option to rejoin?

I’m pretty sure if they lose connection, it still fires, but the catch is if they lose connection not because of their internet. The “Lost connection to the game server” message can pop up if the server has crashed, and if the server crashes BEFORE data saves, then the player’s will lose their progress.

So, I see a two reasons why the data might not be saving.

  1. The server’s are crashing.
  2. There’s a problem with your code.
1 Like

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

Hello sorry to start this topic again, but I’ve received some more bug reports on my group wall and every single time the players lose data when they lose connection. But I’ve never seen or heard anyone say a full server got crashed. So the only explanation would be that roblox doesn’t fire it

In my code there’s nothing that could cause this. So it just has to be because Roblox shows a different behavior when players get disconnected instead of leaving normally.

Generally, a player won’t know the server crashed. There’s literally nothing different you see when the server crashes and when the player’s internet stops working. But, I think it could still also maybe be something with your code. Could you send atleast some part of it?

But if servers crash I would be able to see that in the developer stats right?
And yeah sure, here is the code. But it’s not a lot different from most data-saving tutorials and stuff.
The only difference is that it has even more safety built in.

local DataServiceRef = game:GetService("DataStoreService")

--the data id's are just for the data-tags (like "Stage" or "Pets")

Module.SaveData = function(player,dataid,data)

	--when players join and their data doesn't get loaded correctly they get kicked with a message
	-- only if their data does get loaded correctly then it can also get saved, for that there is a DataCheck value
	if player.DataCheck[dataid.."Check"].Value == true then
		for i = 1,5,1 do --trying it multiple times, for if the data saving would fail
			local succes,err = pcall(function() --in pcall for even more safety
				DataServiceRef:GetDataStore("WorldObbyData"):SetAsync(player.UserId.."-"..dataid,data)
			end)
			if succes then --if there was succes
				break --break out of the loop
			end
			if err then --if something went wrong
			--I'm also using GameAnalytics so if there would be any errors I would see them
				warn("data saving problem: "..err)
				wait(i*2) --waiting, longer every time and then trying again
			end
		end
	end
end