Fixed indents, nothing else.
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for i,v in pairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(v)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:Clone().Parent = plr:WaitForChild("StarterGear")
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local toolsToSave = {}
for i,v in pairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
end)
Analysis:
-
pcall
being incorrectly used and utilized, they always return something, more info here - Non-capitalized
connect
is no longer canon, useConnect
, although both works -
SetAsync()
is risky and can lose data upon malfunction - Failure of saving or loading does not have any safety nets
Therefore an example of correct usage of pcall
with return
should be:
-- old example of mine
local success, data = pcall(function()
return ds:GetAsync(key)
end)
-- new example
local success, data = pcall(ds.GetAsync, ds, key)
- If a player had their tool equipped, this won’t be saved.
-
UserId
is incorrectly capitalized, butuserId
is the deprecated version - A magic character was used in a string and the key may fail