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

Why is this data save script now saving the tools in a player's backpack and startergear?

Asked by
BMWLux 0
4 years ago
Edited by JesseSong 4 years ago
local Data = game:GetService("DataStoreService"):GetDataStore("Inventory")

game.Players.PlayerAdded:Connect(function(player)
    local SavedStuff
    local suc, err = pcall(function()
        SavedStuff = Data:GetAsync(player.UserId)
    end)
    if SavedStuff == nil then
        for i,v in pairs(SavedStuff)do
            if game.ServerStorage.Tools:FindFirstChild(v)~= nil then
                game.ServerStorage.Tools:FindFirstChild(v):Clone().Parent = player.StarterGear
                game.ServerStorage.Tools:FindFirstChild(v):Clone().Parent = player.Backpack
            end
        end
    end
    player.CharacterRemoving:Connect(function()
        player.Character.Humanoid:UnequipTools()
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local Table = {}
    for i,v in pairs(plr.Backpack:GetChildren())do
        table.insert(Table,v.Name)
    end
    if Table ~= nil then
        pcall(function()
            Data:SetAsync(plr.UserId,Table)
        end)
    end
end)
game:BindToClose(function(plr)
    for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
        coroutine.wrap(function()
            local Table = {}

            for i,v in pairs(plr.Backpack:GetChildren())do
                table.insert(Table,v.Name)
            end

            if Table ~= nil then
                pcall(function()
                    Data:SetAsync(plr.UserId,Table)
                end)
            end
        end)()
    end
end)

Re-edited by JesseSong!

0
your question isn't specific enough, but if you're wondering why it's saving backpack it's because you have a table you're saving that inserts the children of backpack into it, btw if you have a tool equipped and try to save it, it won't work because equipped tools reside in character rather than backpack Robowon1 323 — 4y

Answer this question