I made an item saving/loading script for someone recently, and it works, for the most part. Recently he told me that, though it does save the items, it occasionally duplicates them or removes them. I've tried examining it, but I can't find much of a reason for either problem.
The tools are stored in ReplicatedStorage, and copied into the player's inventory.
local PlayersItems = game:GetService("DataStoreService"):GetDataStore("Items")
function SaveData(Player)
local Items = {}
for i,Item in ipairs(Player.Backpack:GetChildren()) do
if game.ReplicatedStorage:FindFirstChild(Item.Name) ~= nil then
if game.ReplicatedStorage[Item.Name]:IsA("Tool") then
table.insert(Items, Item.Name)
end
end
end
PlayersItems:SetAsync(Player.UserId, Items)
end
function LoadData(Player)
if PlayersItems:GetAsync(Player.UserId) ~= nil then
for i,ToolName in ipairs(PlayersItems:GetAsync(Player.UserId)) do
local Tool = game.ReplicatedStorage[ToolName]
Tool:Clone().Parent = Player.Backpack
end
end
end
game.Players.PlayerAdded:connect(function(Player)
game.Players:WaitForChild(Player.Name)
Player:WaitForChild("Backpack")
LoadData(Player)
Player.CharacterAdded:connect(function()
LoadData(Player)
end)
game.Players.PlayerRemoving:connect(function(Player)
SaveData(Player)
end)
Player.Backpack.ChildAdded:connect(function()
SaveData(Player)
end)
end)

1 comment
WaitForChild only yields for a max of 5 seconds. If they have an extremely slow connection it may not work. That's not the problem though, just something to note.