How do I save tools in a datastore?

I have been trying to figure out datastores for a long time, but when i try to save tools in the player it does not work and it does not give me an error when I test it, but the leaderstats datastore works fine.

This is the datastore script, its kinda long.

local DSS = game:GetService("DataStoreService")
local DS1 = DSS:GetDataStore("DS1")
local toolsDS = DSS:GetDataStore("toolsDS")

local toolsFolder = game.ServerStorage.Tools

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 1000
	
	local playerId = "Player_" .. player.UserId
	
	local toolsSaved = toolsDS:GetAsync(playerId) or {}
	
	for i, v in pairs(toolsSaved) do
		if toolsFolder:FindFirstChild(toolsSaved) then
			toolsFolder[v]:Clone().Parent = player.Backpack
			toolsFolder[v]:Clone().Parent = player.StarterGear
		end
	end
	
	player.CharacterRemoving:Connect(function(char)
		char.Humanoid:UnequipTools()
	end)
	
	local data
	local success, errormessage = pcall(function()
		data = DS1:GetAsync(playerId, data)
	end)
	if success then
		coins.Value = data
		print("Data Successfully Saved")
	else
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerId = "Player_".. player.UserId
	
	local toolsOwned = {}
	
	for i, v in pairs(player.StarterGear:GetChildren()) do
		table.insert(toolsOwned, v.Name)
	end
	
	local success2, errormessage2 = pcall(function()
		toolsDS:SetAsync(playerId, toolsOwned)
	end)
	
	if success2 then
		print("Data Successfully Saved")
	else
		warn(errormessage2)
	end
	
	local data = player.leaderstats.Coins.Value

	local success, errormessage = pcall(function()
		DS1:SetAsync(playerId, data)
	end)
	
	if success then
		print("Data Successfully Saved")
	else
		warn(errormessage)
	end
end)

Hey bloxer1243!

Objects can not be stored as datastore values, you can only use basic values such as numbers string or boolean, they may also be in a hierarchical structure such as a table or a dictionary.

My suggestion is that you assign each tool an ID and save that ID into their datastore.

Hope this helped!

If you need more info on datastore other than what RepValor stated, I would reccomend looking at the Developer API Here For example, In order to set the value of new data, use SetAsync And in order to access data from the datastore use GetAsync

I found one of my old games that i abandoned and I saw that it had a working tool saver, so now it works. Probobly should have check this before I posted.

hi! just wondering if can you put the script here?

I think this was the one that worked for me.

local DSS = game:GetService("DataStoreService")
local toolDS = DSS:GetDataStore("ToolDS")

local toolsFolder = game.ReplicatedStorage.WoodTools

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

game.Players.PlayerRemoving:Connect(function(player)
	local toolsOwned = {}
	for i, toolInBackpack in pairs(player.Backpack:GetChildren()) do
		table.insert(toolsOwned, toolInBackpack.Name)
	end
	local success, errormessage = pcall(function()
		toolDS:SetAsync(player.UserId.."-Tools", toolsOwned)
	end)
	if success then
		print("Data Successfully Saved")
	else
		warn(errormessage)
	end
end)
4 Likes