Hello, my save tool script is not working. I was wondering if anyone could help me fix it! Thanks for the help!
Here is my script:
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData")
local toolsFolder = game.ServerStorage.ToolsFolder
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()
local tableOfTools = {}
for i, v in pairs(plr.Backpack:GetChildren()) do
table.Insert(tableOfitems,v.Name,#tableOfitems+1)
end
toolsDS:SetAsync(plr.UserId .. "-tools", tableOfTools)
end)
end)
First thing: It’s always important to encase your DataStore functions inside pcalls, otherwise the script would have a chance of breaking if the Data attempting to load is returned back as nil
Second thing: Are API Services turned on that should enable the DataStoreService?
local dss = game:GetService("DataStoreService")
local toolsDS = dss:GetDataStore("ToolsData")
local toolsFolder = game.ServerStorage.ToolsFolder
game.Players.PlayerAdded:Connect(function(plr)
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)
It could be possible that tableOfitems may still be nil if there’s nothing found inside the Player’s Backpack, maybe you should reference the Player’s StarterGear instead?
That wouldn’t make sense though if nothing printed
local function AddPlayer(plr)
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
There should be something that Outputted as either as print, or a warning message in the Output