Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is the datastore occasionally duplicating or removing items?

Asked by 7 years ago

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)
0
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. EzraNehemiah_TF2 3552 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

It looks like your player or the ReplicatedStorage has the same item twice.

0
Can you please elaborate as to why? Crystakyl 20 — 7y
0
Cloning usually clones it more then once. (its annoying) DeveloperSolo 370 — 7y
Ad

Answer this question