Game / Player Event to save Data before close

Hello, I am doing my database setup and I have a little problem with the event PlayerRemoving, there is no time to save data I guess.

game:GetService("Players").PlayerRemoving:Connect(function(p)
	print(0)
	local getData = database:GetAsync(p.UserId)
	print(1)
	local decode = game:GetService("HttpService"):JSONDecode(getData)
	print(2)
	local sendData = {
		banned = decode["banned"],
		divisions = {
			CMU = decode["divisions"]["CMU"],
			PHANTOM = decode["divisions"]["PHANTOM"]
		},
		name = p.Name,
		points = p.policePoint.Value,
		rank_bsp = p.Ranks.bspRank.Value,
		reason_ban = decode["reason_ban"],
		tempban_time = decode["tempban_time"]
	}
	print(3)
	database:SetAsync(p.UserId, game.HttpService:JSONEncode(sendData))
	print(4)
end)
2 Likes

Can you further clarify? The script looks fine. If you’re looking toward debugging and the Data Save progress does not complete in Studio, then you should use Game:GetService("Players").username:Kick().

2 Likes

Actually I tried to Debug with prints but it doesn’t go above 0 so I think it comes from the event right? It’s as if there wasn’t time to make the request.

I see the issue. You must and should use Game:GetService("Players").username:Kick() if you want to debug data save. It actually works fine when in-game, but not in Studio because IIRC no clients on it will instantly shutdown the server (Kick bypasses it I believe).

1 Like

I don’t understand what you I do :(. I mean, when I “kick”?

Just try using Game:GetService("Players").username:Kick() in the command bar while playtesting and you’ll see.

What??? It’s doing when I leave the game, how can I kick and leave??? It does not make sense.

I don’t understand what you said, but you should try doing the following said. Leave removes the player, so is Kicking. PlayerRemoving is an event that is initiated by player removal (Kick and Leave).

Leaving on Roblox Studio does not help you with debugging PlayerRemoving. It actually instantly shut down the local test server. You should try kicking which bypasses that.

When you are in the game and not on Roblox Studio, it actually does save when you leave.

You have to try the method in order to know it works.

1 Like

You tell me to kick, but I LEAVE the game, so explain to me where you want me to kick lol? Like I’m leaving the game I agree it comes back to kick but it doesn’t make any sense what you’re saying.

But in-game I did not test, I will try it later.

While playtesting, run the command in this spot
2d4e833ff65370a7861e1fa3d5c04ef1aeda6e4d

1 Like

I know… Nvm it does not matter :>.

I am solving my problem by another way, thank you again :).