Player Removing Event Not Working

Hello, I have a simple Leaderstats script with save system in it.

How ever when player exits the game, event doesn’t fires.

The script is not local script.

And of course accessing the data is enabled in settings.

Script:

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("V1.0")

game.Players.PlayerAdded:Connect(function(plr)
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = plr
	
	local Record = Instance.new("IntValue")
	Record.Name = "Record"
	Record.Parent = Stats
	Record.Value = ds:GetAsync(plr.UserId, plr.leaderstats.Record.Value) or 0
	
	local Score = Instance.new("IntValue")
	Score.Name = "Previous"
	Score.Parent = Stats
	

	
end)



game.Players.PlayerRemoving:Connect(function(plr)
 ds:SetAsync(plr.UserId, plr.leaderstats.Record.Value)
 print("DataSaved")
end)
3 Likes

Hello,
The event is not being fired, because I bet you are just testing it in the studio Solo Play. When you end the Solo Play session, the script stops running and it won’t have enough time to react.

Although I have found a little life-hack for this. Instead of clicking the Stop button, just type the following command into Command Bar: game:GetService("Players").LocalPlayer:Kick(). This is a thing that has been working for me every time I wanted to test DataStores inside of studio Solo Play.

25 Likes

Your a life saver thanks so much!!!

2 Likes