Hello, my save tool script is not working, and I was wondering if anyone could help me fix it, there is one error (screenshot below) and I need help fixing that, so here is my code and error.
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData")
local toolsFolder = game.ReplicatedStorage.ToolsFolder
local function AddPlayer(plr)
local toolsSaved = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
print("This line should at least work")
local Data
local success, whoops = pcall(function()
Data = toolsDS:GetAsync(plr.UserId .. "-tools") or {}
end)
if success and Data then
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
else
warn("Something happened while obtaining to get the Player's Data: ", whoops)
end
plr.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
local tableOfTools = {}
for i, v in pairs(plr.Backpack:GetChildren()) do
table.Insert(tableOfitems,v.Name,#tableOfitems+1)
end
local success, whoops = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", tableOfTools)
end)
if success then
print("Data saved successfully!")
else
warn(whoops)
end
end)
end
game.Players.PlayerAdded:Connect(AddPlayer)
for _, Plr in pairs(game.Players:GetPlayers()) do
AddPlayer(Plr)
end
When you do not put in the 3rd argument of table.insert, it will automatically add it to the “end” of the table (so +1).
“Appends the provided value to the end of array t. The optional pos value defaults to #t+1, meaning that value is inserted at the end of array t unless otherwise specified.”
There was no errors, however the saved data successfully did not print
or i, v in pairs(plr.Backpack:GetChildren()) do
table.insert(tableOfTools, v.Name)
end
local success, whoops = pcall(function()
toolsDS:SetAsync(plr.UserId .. "-tools", tableOfTools)
end)
if success then
print("Data saved successfully!")
else
warn(whoops)
end
You should save data in PlayerRemoving instead. Also, you should not be saving tools from the Backpack. I would recommend storing the tool information in a table instead.