Why is it Removing?

Yo wsp everyone I got this Script that saves my Backpack tool but somewhy sometimes a tool gets removed and litarilly what ever I tried with prints and without it wouldn’t tekll me why I use this system and in another game of mine the same thing happened anyone know why or how I can save tools instead:?

local itemsFolder = game:GetService('ServerStorage'):WaitForChild('Saved')
local players = game:GetService('Players')
local dataStoreService = game:GetService('DataStoreService')
local mainStore = dataStoreService:GetDataStore('Main')

local function playerAdded(plr)
	repeat
		task.wait()
	until plr.Character
	task.wait(0.1)

	local success, inventory = pcall(function()
		return mainStore:GetAsync(plr.UserId)
	end)

	if success and inventory then
		for _, name in pairs(inventory) do
			local item = itemsFolder:FindFirstChild(name)
			if item then
				local clone = item:Clone()
				clone.Parent = plr.Backpack
			end
		end
	end
end

local function playerLeft(plr)
	local backpack = plr.Backpack
	local char = plr.Character
	local itemsTab = {}

	if char then
		for _, object in pairs(char:GetChildren()) do
			if object:IsA('Tool') then
				table.insert(itemsTab, object.Name)
			end
		end
	end

	for _, object in pairs(backpack:GetChildren()) do
		if object:IsA('Tool') then
			table.insert(itemsTab, object.Name)
		end
	end

	local success, err = pcall(function()
		return mainStore:SetAsync(plr.UserId, itemsTab)
	end)

	if not success then
		warn("Error saving data for player " .. plr.UserId .. ": " .. err)
	end
end

players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerLeft)

You can also add game:BindToClose() and run the playerLeft function there as well. I am assuming you’re testing in studio, and studio is weird with data saving unless you made it safe through a Module or through you making it safe yourself, try in the actual Roblox Player.

1 Like

well I did it but to make sure it remopves tools not when I leave but suddenly when I play and for example hold the tool with a 0.1 sec lag like I’m sure if u try it will happen too

I’ve tried making Tool saving systems in the past and what worked for me is just keeping track of what tools the player has and then when it comes time to save their tools, serialise that table

So make a function, RegisterTool(player, tool) that adds the tool to a dedicated table for that player (and ensure that it is a unique tool, if they equip and unequip a tool it shouldnt duplicate)

Then listen for tools added: Character.ChildAdded and Backpack.ChildAdded
As well as when the player joins, check their backpack and character for tools that may have appeared before the script checked

Then sometimes tools are deleted and you don’t want them to persist, so for each tool listen for: Tool.Destroying and Tool:GetPropertyChangedSignal(“Parent”) and check if the parent is nil
If deleted, just remove from the table in memory

When a player leaves, serialise the table (in your case, make a new table that just contains the names of the tools, but you can also convert them into indexes for better datastore efficiency) and delete the tool table from memory

2 Likes