Help with Tool saving Data Store

Hello, i have Data Store that saves tools even u leave and join back, i noticed it only saves if u pick it up, how would i make it save if it gets cloned to your backpack?


local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData1")

local toolsFolder = game.ServerStorage:WaitForChild("ToolsFolder1")

game.Players.PlayerAdded:Connect(function(plr)
	
	local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
	
	for i, toolSaved in pairs(toolsSaved) do
			
		if toolsFolder:FindFirstChild(toolSaved) then 
			
			toolsFolder[toolSaved]:Clone().Parent = plr.Backpack
			toolsFolder[toolSaved]:Clone().Parent = plr.StarterGear 
		end
	end
	
	plr.CharacterRemoving:Connect(function(char)
    	
		char.Humanoid:UnequipTools()
    end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	
	local toolsOwned = {}
	
	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
		
		table.insert(toolsOwned, toolInBackpack.Name)
	end
	
	local success, errormsg = pcall(function()
		
		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end)

game:BindToClose(function(plr) -- Runs whenver the server is about to shut down/stop.

	local toolsOwned = {}

	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do

		table.insert(toolsOwned, toolInBackpack.Name)
	end

	local success, errormsg = pcall(function()

		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end)
2 Likes

how would i make it save if it gets cloned or put into backpack any other way than picking it up?

1 Like

im a little bit confused about what your goal is, could you explain a bit detail about what are you trying to achieve?

Yeah sure, basically I have a shop system and when u purchase a tool it gets cloned to your backpack and I tried using this data store but it only saves the tool when u pick it up, it doesn’t save it if it gets cloned so how would I save it if it gets cloned u know what u mean? Tell me if I explained it bad lol

do you mean that it only saves when youre equipping it?

No it only saves if u pick it up by like walking over it, if it gets cloned into your inventory I guess it doesn’t count as saving it

Oh maybe ur right u might have to equip it for it to save, do you know how to make it where it saves even if u don’t equip it?

maybe you can try this

game.Players.PlayerRemoving:Connect(function(plr)
    plr:WaitForChild("Character"):WaitForChild("Humanoid"):UnequipTools()
	
	local toolsOwned = {}
	
	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do
		
		table.insert(toolsOwned, toolInBackpack.Name)
	end
	
	local success, errormsg = pcall(function()
		
		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end)

so you unequip the tool if the player equips it
It will automatically put the tool inside the backpack

and save it,

Let me know if theres any error

hope this helps

I will try this tomorrow, I’ll let you know if it works thanks

1 Like

i dont think it saves still, when i buy the tool it goes into ur inventory but doesnt save

Remember DataStores are server-sided and when you move it to your own backpack, you’re on client-side, so the server thinks there’s nothing on your backpack. Everything with the code is correct, you just need to switch to server-side while changing the tool’s parent with this icon:
image

image
image

Also, if you are planning on creating a UI shop, don’t save data with a LocalScript, instead use a RemoteEvent to handle saving on the server.

For a last a recommendation, to not repeat code, create a function and call it everytime you save data.

local function SaveData(plr)
	local toolsOwned = {}

	for i, toolInBackpack in pairs(plr.Backpack:GetChildren()) do

		table.insert(toolsOwned, toolInBackpack.Name)
	end

	local success, errormsg = pcall(function()

		toolsDS:SetAsync(plr.UserId .. "-tools", toolsOwned)
	end)
	if errormsg then warn(errormsg) end
end

Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
	for _, plr in ipairs(Players:GetPlayers()) do
		SaveData(plr)
	end
end)
3 Likes