Datastore tool saving. but does not save from char (equipped tools)

ok so. i got the saving down. but when its equipped and I leave the game it does not save. i been trying to make it work but don’t know how to

changing from datastore to the datastore2 module, putting the items into backpack before the player leaves, saving both backpack and char tools. but still losing stuff when I rejoin if its in char

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerItems")

-- Helper function to save items
local function savePlayerItems(player)
	local inventory = {}

	local backpack = player:FindFirstChild("Backpack")
	if backpack then
		for _, item in ipairs(backpack:GetChildren()) do
			if item:IsA("Tool") and item:FindFirstChild("saveable") and item.saveable.Value == true then
				table.insert(inventory, item.Name)
				item:Destroy() -- Unequip and remove from backpack
			end
		end
	end

	local character = player.Character
	if character then
		for _, item in ipairs(character:GetChildren()) do
			if item:IsA("Tool") and item:FindFirstChild("saveable") and item.saveable.Value == true then
				table.insert(inventory, item.Name)
				item:Destroy() -- Unequip and remove from character
			end
		end
	end

	-- Save inventory to DataStore
	local success, err = pcall(function()
		PlayerDataStore:SetAsync(player.UserId, inventory)
	end)

	if not success then
		warn("Failed to save player items for " .. player.Name .. ": " .. err)
	end
end

-- Helper function to load items
local function loadPlayerItems(player)
	local inventory = {}

	local success, err = pcall(function()
		inventory = PlayerDataStore:GetAsync(player.UserId) or {}
	end)

	if not success then
		warn("Failed to load player items for " .. player.Name .. ": " .. err)
		return
	end

	local backpack = player:FindFirstChild("Backpack")
	if not backpack then
		warn("Backpack not found for " .. player.Name)
		return
	end

	for _, itemName in ipairs(inventory) do
		local tool = game.ReplicatedStorage.saveabletool:FindFirstChild(itemName)
		if tool then
			local clonedTool = tool:Clone()
			clonedTool.Parent = backpack
		else
			warn("Tool not found in saveabletool for " .. itemName)
		end
	end
end

-- Player added event
game.Players.PlayerAdded:Connect(function(player)
	local hasReceivedTools = false -- Track if tools have been given already

	player.CharacterAdded:Connect(function()
		if not hasReceivedTools then
			loadPlayerItems(player)
			hasReceivedTools = true -- Prevent giving tools again after respawning
		end
	end)
end)

-- Player removing event
game.Players.PlayerRemoving:Connect(function(player)
	local backpack = player:FindFirstChild("Backpack")
	local character = player.Character

	-- Ensure tools equipped in character are moved to backpack
	if character then
		for _, item in ipairs(character:GetChildren()) do
			if item:IsA("Tool") and item:FindFirstChild("saveable") and item.saveable.Value == true then
				item.Parent = backpack -- Move to backpack for consistent saving
			end
		end
	end

	-- Save tools from both backpack and character
	savePlayerItems(player)
end)
1 Like