Backpack Empty on Character Removing

I am currently trying to make the tools in the player inventory save, so when they die it won’t just go away. But I am encountering an issue where if player:LoadCharacter happens the tools won’t come back.

So naturally, I tried to check why can’t Player.CharacterRemoving work, I printed the numbers of children in the Backpack and it was 0, even though I had 2 tools in my inventory.

When humanoid dies and respawns, tools come back as intended.
Video featuring the function:

But when load character it does not.
Video featuring the problem:

Printed:
image

The code I am using:

local function OnPlayerAdded(Player)
	local SavedTools = {}

	local function SaveTools(Character)
		-- Save all tools from Backpack
		for _, Tool in pairs(Player.Backpack:GetChildren()) do
			if Tool:IsA("Tool") then
				table.insert(SavedTools, Tool:Clone())
			end
		end

		-- Save any tool currently equipped
		for _, Tool in pairs(Character:GetChildren()) do
			if Tool:IsA("Tool") then
				table.insert(SavedTools, Tool:Clone())
			end
		end
	end

	local function OnCharacterAdded(Character)
		-- Restore saved tools
		for _, Tool in pairs(SavedTools) do
			Tool.Parent = Player.Backpack
		end
		SavedTools = {}

		-- Connect Died event to save tools on death
		Character:WaitForChild("Humanoid").Died:Connect(function()
			SaveTools(Character)
		end)
		
		
		
		Player.CharacterRemoving:Connect(function()
			print("Backpack children:", #Player.Backpack:GetChildren())
		end)
		
		
		
	end

	Player.CharacterAdded:Connect(OnCharacterAdded)
	
	
	
	
end

-- Handle players who are already in the game
for _, Player in pairs(Players:GetPlayers()) do
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Any help is appreciated! :happy3:

I believe it is with the signal speed (workspace.SignalBehaviour) but changing it is not recommended, you could make a module script with a custom LoadCharacter function which would save the tools before calling it.

2 Likes
ServerScript
--ServerScript in ServerScriptService
local Players = game:GetService("Players")

local function OnPlayerAdded(Player)
	local SavedTools = {}

	local function SaveTools(Character)
		for _, Tool in pairs(Player.Backpack:GetChildren()) do
			if Tool:IsA("Tool") then
				Tool.Parent = nil
				table.insert(SavedTools, Tool)
			end
		end

		for _, Tool in pairs(Character:GetChildren()) do
			if Tool:IsA("Tool") then
				Tool.Parent = nil
				table.insert(SavedTools, Tool)
			end
		end
	end

	local function OnCharacterAdded(Character)
		for _, Tool in pairs(SavedTools) do
			Tool.Parent = Player.Backpack
		end

		SavedTools = {}

		local humanoid = Character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			SaveTools(Character)
		end)
	end

	Player.CharacterAdded:Connect(OnCharacterAdded)
end

for _, Player in pairs(Players:GetPlayers()) do
	OnPlayerAdded(Player)
end

Players.PlayerAdded:Connect(OnPlayerAdded)
1 Like

Thanks for the reply and your solution but it doesn’t seem to fix the issue. :LoadCharacter still removes my inventory clean.

Thank you for your response, I have created a bindable event, firing every time when I want to use the LoadCharacter function, which solves the issue I am encountering

Thank you everyone who have replied to this forum!

:heart:

1 Like

Well, that’s truly odd. That was fully tested and is still working fine.
I guess you have other problems..

1 Like

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