CharacterRemoving never firing

I’m trying to make a a CharacterRemoving event detect when a player is leaving while also having access to their character. The problem is, my CharacterRemoving event never fires even though I am leaving.

I’ve put a print statement right below player.CharacterRemoving and nothing has happened no matter the condition.

Players.PlayerRemoving:Connect(function(player)
		local key = tostring(player.UserId)
		player.CharacterRemoving:Connect(function(character)
			local function FindTools()
				local tools = {}
				for i,v in player.Backpack:GetChildren() do
					table.insert(tools,v.Name)
				end
				local tool = character:FindFirstChildOfClass("Tool")
				if tool then
					print(tool)
					table.insert(tools,tool.Name)
				end
				return tools
			end
			local toolNames = FindTools()
			for i,v in toolNames do print(i,v) end
			local successTools,resultTools = pcall(function()
				return Data:SetAsync("tools"..key,toolNames)
			end)
		end)
		local successMoney,resultMoney = pcall(function()
			return Data:SetAsync("money"..key,player.leaderstats.Money.Value)
		end)
		local successBananas,resultBananas = pcall(function()
			return Data:SetAsync("bananas"..key,player.Bananas.Value)
		end)
		local successFish,resultFish = pcall(function()
			return Data:SetAsync("fish"..key,player.Fish.Value)
		end)
		local successUpgrades,resultUpgrades = pcall(function()
			return Data:SetAsync("upgrades"..key,sessionData[player]["Upgrades"])
		end)
	end)

Any thoughts?

1 Like

The Character always gets removed before the player, that is why this isn’t working, because the character is already removed by the time PlayerRemoving is ran.

I noticed you check the players backpack, you could handle all of that when playerremoving is ran.

2 Likes

Am I supposed to have player.CharacterRemoving in the PlayerAdded event?

Well CharacterRemoving also fires when the character respawns so that may cause unexpected issues in your code.

I don’t typically mess with CharacterRemoving, the only way I personally have saved character related data before they left the game was for their health, and having a script detect when its changed and store it in a table so when the player leaves we just add that to the datastore.

You may be able to accomplish a similar effect, from what I see it seems like you are trying to save stuff from the players backpack, and also trying to find any tools the player may have been holding, that was not in the backpack.

I again don’t typically mess with this stuff but you may be able to detect when a player swaps to a tool then store the type of tool in a table temporarily, (likely if the script finds a tool) and take it out when they let go (so when it leaves the character).

Then when the player leaves the game, save the stuff in their backpack, and also check if there was a tool in the table and if it was, just add that to the backpack datastore. and obviously make sure to set the player in the table to nil afterwards.

Again, I hardly mess with this type of stuff.

Nah you were right, but I all I had to do was put an if statement to check if the player’s health was greater than 0.

1 Like

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