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

[Solved] My backpack datastore script doesn't work properly?

Asked by
qfoxb 26
2 years ago
Edited 2 years ago

[Solved]

so i'm trying to save the players backpack tools, and when the player leaves, the backpack saves the first time, but after the player joins again, then leaves again, the backpack gets cleared. as well as the currently held item not getting transferred

DataStore = game:GetService("DataStoreService"):GetDataStore("backpack")
local tol = game.ServerStorage.PurchaseableTools
game.Players.PlayerAdded:Connect(function(player)
--    print("TEST AHHHH")
    local Backpack = player:WaitForChild("Backpack")
    local data = DataStore:GetAsync("Back-"..player.UserId)
    if data then
        for i,v in pairs(data) do
            if game.ServerStorage:WaitForChild("PurchaseableTools"):WaitForChild(v) then

                local Tool = game.ServerStorage.PurchaseableTools:FindFirstChild(v)
                local NewTool = Tool:Clone()
                NewTool.Parent = player.Backpack -- We do not have to insert into StarterPack because player cannot reset
            end
        end
    end
end)
function Save(player)
    local c1 = game.Workspace:FindFirstChild(player.Name)
    if c1:FindFirstChild("Humanoid") then
        for i,v in pairs(c1:GetChildren()) do
            if v.ClassName == "Tool" then
                v.Parent = player.Backpack -- We do not have to insert into StarterPack because player cannot reset
            end
        end
    end
        local inventory = {}
        for i,v in pairs(player.Backpack:GetChildren()) do
            if tol:FindFirstChild(v.Name) then

                table.insert(inventory,v.Name)

            end

        end
        local success,errormessage = pcall(function()
            DataStore:UpdateAsync("Back-"..player.UserId, function(CurrentState) 
                return inventory or CurrentState  
            end)
        end)    
        if success then 
            print("BackpackDataStore: Data Saved Successfully for "..player.Name)

        else
            warn("BackpackDataStore: Data Saving Failed for Player "..player.Name)
            -- TODO: warn player of failed save
        end
    end

    game.ReplicatedStorage.RemoteEvents.Save.OnServerEvent:Connect(function(player) --bad workaround
        Save(player)
    end)

game.Players.PlayerRemoving:Connect(function(player)
    local inventory = {}
    for i,v in pairs(player.Backpack:GetChildren()) do
        if tol:FindFirstChild(v.Name) then

            table.insert(inventory,v.Name)

        end

    end
    local success,errormessage = pcall(function()
        DataStore:UpdateAsync("Back-"..player.UserId, function(CurrentState) 
            return inventory or CurrentState  
        end)
    end)    
    if success then 
        print("BackpackDataStore: Data Saved Successfully for "..player.Name)

    else
        warn("BackpackDataStore: Data Saving Failed for Player "..player.Name)
        -- read old todo
    end

end)
0
Solved. Needed player.CharacterAdded:Wait() qfoxb 26 — 2y

Answer this question