PlayerRemoving event not being listened to

Hi, I’m a novice developer making a ‘PLS JOIN’ sort of game where you can advertise your groups and such. My issue is my PlayerRemoving event isn’t listening to whenever it fires so data couldn’t be saved. I’m using IncrementAsync() instead of SetAsync() to make sure DataStore requests aren’t throttled. Here is the script:

--Whenever I mention X0, I mean the player's GroupBux (This is to prevent exploiters from understanding what's in the client-sided scripts)
local players = game:GetService('Players')
local dss = game:GetService('DataStoreService')
local DataStores={
	Raised = dss:GetOrderedDataStore('Raised');
	Donated = dss:GetOrderedDataStore('Donated');
	GroupBux = dss:GetOrderedDataStore('GroupBux');
	FirstPlayerData = {};
}
players.PlayerAdded:Connect(function(player)
	local lstats = Instance.new('Folder',player)
	lstats.Name = 'leaderstats'
	local donated = Instance.new('IntValue',lstats)
	donated.Name = 'Donated'
	local raised = Instance.new('IntValue',lstats)
	raised.Name = 'Raised'
	local playerData = {[1]='Raised',[2]='Donated',[3]='GroupBux'}
	local received = {}
	for i = 1, #playerData do
		local currentDataStore = DataStores[playerData[i]]::OrderedDataStore
		repeat
			local options = Instance.new('DataStoreGetOptions')
			options.UseCache = true
			local suc, object = xpcall(function()
				local result = currentDataStore:GetAsync(player.UserId,options)
				received[playerData[i]] = result
				return result
			end,function(err)
				warn(err)
				wait(.1)
			end)
		until suc or not game.Players:FindFirstChild(player.Name,false)
	end
	donated.Value = received[2] or 0
	raised.Value = received[1] or 0
	player:SetAttribute('X0',received[3]or 0)
	DataStores.FirstPlayerData[player.UserId]={Raised=received[1],Donated=received[2],GroupBux=received[3]}
end)


players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:FindFirstChild('leaderstats')
	if leaderstats then
		for key, dataStore in pairs(DataStores) do
			if dataStore ~= DataStores.FirstPlayerData then
				local value = leaderstats:FindFirstChild(dataStore.Name)
				if value then
					local difference = value.Value - DataStores.FirstPlayerData[player.UserId][dataStore.Name]
					repeat
						local success, err = pcall(function()
							dataStore:IncrementAsync(player.UserId, difference)
						end)
						if not success then
							warn(err)
							wait(0.4)
						end
					until success
				end
			end
		end
	end
end)

I know this because there are no print statements raising. I would very much appreciate if you could help me out and tell me what is going on.

Are you testing this in a server environment where there is only one player left, or in a busier session with several? You’re looking at the possibility that you have not accounted for what to do when the server is about to shutdown(which sometimes doesn’t give time to execute PlayerRemoving fully!).

This, or the Script is stuck in the repeat loop from the PlayerAdded Event. You could try adding some prints to check that. Did you check for errors in the output?

I tested it with 2 alternate accounts and saw no errors in the console, even by debugging it with print statements doesn’t show the issue.

Try this and see if you get any warnings:

players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:FindFirstChild('leaderstats')
	if leaderstats then
		for key, dataStore in pairs(DataStores) do
			if dataStore ~= DataStores.FirstPlayerData then
				print("dataStore ~= DataStores.FirstPlayerData")
				local value = leaderstats:FindFirstChild(dataStore.Name)
				if value then
					local difference = value.Value - DataStores.FirstPlayerData[player.UserId][dataStore.Name]
					repeat
						local success, err = pcall(function()
							dataStore:IncrementAsync(player.UserId, difference)
						end)
						if not success then
							warn(err)
							wait(0.4)
						end
					until success
				else
					warn("value not found")
				end
			end
		end
	else
		warn("leaderstats not found")
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.