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!
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.
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.
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.