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

How do I get DataStore to retain a player's Backpack? (followup)

Asked by 7 years ago

Hello!

Looking to see if I can get further assistance with saving a player's backpack to the datastore. My original script that already saves the leaderboard is:

game.Players.PlayerAdded:connect(function(player)
        local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")

    player:WaitForChild("leaderstats")
    wait(1)
    local stats = player:FindFirstChild("leaderstats"):GetChildren()
    for i = 1, #stats do            
    stats[i].Value = datastore:GetAsync(stats[i].Name)
    print("stat number "..i.." has been found")

    stats[i].Changed:connect(function() 
local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
    datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
    print("saved data number "..i)

end
print("Stats successfully saved")   
end)
        end
end)

A kind person pointed out this similar thread: https://scriptinghelpers.org/questions/39874/how-do-i-save-a-players-gearsinventory-when-they-leave

Following that thread's answer/instructions I've created a folder in ServerStorage called "ToolStorage", and I've copied the weapons from my ReplicatedStorage into it. However, it doesn't quite seem to work, I'm positive the error is on my end. And the ServerLog from the Developer Console gives me this error in red: Workspace.DataStoreStatsBackpack:5: attempt to call field 'ToolStorage' (a userdata value)

What am I missing here? Do I have the line improperly done? Is HopperBin still active, I thought it was deprecated?

--Setup the DataStore
local ds = game:GetService("DataStoreService"):GetDataStore("Tools");
local tools = game.ServerStorage.ToolStorage("Bow", "GravityCoil", "RocketLauncher");  --Specify all possible tools

--{[Check and Load]}
game.Players.PlayerAdded:connect(function(plr)

    --Retrieve the potential data
    local data = ds:GetAsync(plr.UserId);

    --Check if it exists
    if data then

        --If it does, loop and clone respective tools.
        for _,v in next,data do
            local tool = tools:FindFirstChild(v);
            if tool then
                tool:Clone().Parent = plr.Backpack;
            end
        end
    end
end)

--{[Collect and Save]}

game.Players.PlayerRemoving:connect(function(plr)

    --Make a table to hold all the data
    local toolList = {};

    --Iterate through the backpack and fill the table in

    for _,v in next,plr.Backpack:GetChildren() do
        --Make sure it's actually a tool

        if v:IsA("Tool") or v:IsA("HopperBin") then
            toolList[#toolList+1] = v.Name;
        end
    end
    --Save the table

    ds:SetAsync(plr.UserId,toolList);
end)

1 answer

Log in to vote
0
Answered by
bno23 30
7 years ago

You took the code snippet:

local tools = game.ServerStorage.ToolStorage; --Specify all possible tools

A little too literally. Remove the parentheses and stuff inside it, and you should be fine.

0
...seriously? :D Hahaha, you're absolutely right. It does indeed work. Thank you so much! Never2Humble 90 — 7y
Ad

Answer this question