Moving equipped tools to backpack when player leaves

i got a saving tool system that is saving all tools inside the players backpack, which had an issue where if the player left holding a tool it wouldnt save

so i made a local script that parented the holding tool to the backpack when the player left, but it still wont save correctly

local script:

	local function onPlayerRemoving()
	print("Player is leaving the game...")

	local character = player.Character
	if character then
		print("Character found: " .. character.Name)
		local tool = character:FindFirstChildOfClass("Tool") 
		if tool then
			print("Tool found: " .. tool.Name .. ". Moving it to the backpack...")
			tool.Parent = backpack -
			print("Tool moved to backpack.")

saving backpack tools:

local playerTools = {}

	-- Check tools in the backpack
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack then
		for _, tool in ipairs(backpack:GetChildren()) do
			if tool:IsA("Tool") then
				table.insert(playerTools, tool.Name)
			end
		end
	end
2 Likes

Well first of all, instead of directly parenting the tools to the characater’s backpack, you could just do:

Character.Humanoid:UnequipTools()

Also it’s likely that the character is gone by the time the PlayerRemoving event has fired, so I would recommend that when a player is added to the game, you add a character removing event that always unequips their tools.

Player.CharacterRemoving:Connect(function(char)
    char:FindFirstChildOfClass("Humanoid"):UnequipTools()
end)

That would mean that you wouldn’t have to work with parenting the tool to the backpack when the player is removed, and all you would have to do with saving.

I’m still a bit hesitant however b/c if your first method didn’t work then I’m not sure that mine would, but I’d still recommend to try anyways.

1 Like

just tried this and it did not work

The tools must be moved to backpack via server script no?

tried this aswell and didnt work

save the data AFTER u put the items in the players inventory

would a wait work? if yes i tried aswell and this the output

18:52:22.615 Player is leaving the game: - Studio
18:52:22.615 Character found:- Studio
18:52:22.615 Tool found: MICROFONE. Moving it to the backpack… - Studio
18:52:22.615 Tool moved to backpack. - Studio
18:52:22.615 Tool unequipped by player:- Studio
18:52:22.615 Animations stopped on unequip. - Studio
18:52:22.795 Disconnect from ::ffff:127.0.0.1|50212 - Studio
18:52:23.808 Saving tools for player : SpeedCoil - Servidor -