Tools not saving | DataStore

Hello!


So I have this code :

local ToolsData = {}


 local function AddToolData(Tool)
	table.insert(ToolsData, Tool.Name)
end

for i, tool in pairs(player.Backpack:GetChildren()) do
	if tool then
		if tool:IsA("Tool") then
			AddToolData(tool)
			task.wait()
		end
	end
end

local Success, ErrorMessage = pcall(function()
	ToolsDataStore:SetAsync(player.UserId, ToolsData)
end)

if Success then
	print("Data successfully saved!")
else
	print("There was an error whilst loading your data. We're sorry!")
	warn(ErrorMessage)
end

But it does not save my tools when rejoining, did I do anything wrong?
Please help! :smiley:

1 Like

You forgot to check if the player’s character has tools. The new script would look like this:

New Script
local ToolsData = {}

local function AddToolData(Tool)
	table.insert(ToolsData, Tool.Name)
end

for i, tool:Tool in pairs(player.Backpack:GetChildren()) do
	AddToolData(tool)
end

for i, instance:Tool in pairs(player.Character:GetChildren()) do
	AddToolData(instance)
end

local Success, ErrorMessage = pcall(function()
	ToolsDataStore:SetAsync(player.UserId, ToolsData)
end)

if Success then
	print("Data successfully saved!")
else
	print("There was an error whilst loading your data. We're sorry!")
	warn(ErrorMessage)
end

game:BindToClose(function()
    task.wait(5)
end)

Unequipped = Tool parent is backpack
Equipped = Tool parent is character

Also, if this solution doesn’t work, please send me the script which loads the tools when the player joins.

1 Like

Still doesn’t work.
Also that’s the entire code for the saving tools. How can I make them ‘load’ ?

1 Like

Instances cannot be stored into Data Stores, you would have to serialize it instead.

local ToolsData = {}

-- tool object data
-- insert property values of the tool
local toolObject = {
    ["Archivable"] = tool.Archivable;
    ["CanBeDropped"] = tool.CanBeDropped;
}

-- add the tool object to the data table
table.insert(ToolsData, toolObject)

When the player rejoins, the tool data will need to be deserialized.

local success, result = pcall(function()
    local data = ToolsDataStore:GetAsync(player.UserId)
    for i, v in pairs(data) do
        if v.CanBeDropped then
            -- create tool
            local tool = Instance.new("Tool")
            tool.Archivable = v.Archivable
            tool.CanBeDropped = v.CanBeDropped
        end
    end
end)

EDIT:

I did not see you were storing the tool’s name. In this case, have a copy of the tool somewhere with the matching name, then clone it when the player rejoins.

Let’s say you were storing a tool named “Sword”, have a copy of the tool in ServerStorage. When the player rejoins, use the data to find if the tool exists, if it does then clone it.

1 Like

So if I understand what you’re saying correctly :

  • Save the name of the player’s tools
  • Search this name in the Tools folder of ServerStorage
  • Clone the tool found
  • Put the clone in the player’s backpack
1 Like

Yes, that is precisely what I meant. It was based on the code you provided, but there are other ways to apprach the same thing. The other being to serialize the tool.

1 Like

Sorry I tried but i’m too dumb to figure out how to do this.

Why did you created 2 posts on the same topic?

Tools not saving | DataStore Problems