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

Why does DataStore:GetAsync return an empty table / nil value every time?

Asked by
appxritixn 2235 Moderation Voter Community Moderator
4 years ago

First, my code:

local DATASTORESERVICE = game:GetService("DataStoreService")
local toolStore = DATASTORESERVICE:GetDataStore("-tools")
game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
        local tools = {}
        for _,v in pairs(player.Backpack:GetChildren()) do
            table.insert(tools,1,v)
            print("Saving: "..v.Name)
        end
        for _,v in pairs(player.Character:GetChildren()) do
            if v:IsA("Tool") then
                table.insert(tools,1,v)
                print("Saving: "..v.Name)
                break
            end
        end
        local own = {}
        for i,v in pairs(game.ServerStorage:GetChildren()) do
            if tools:FindFirstChild(v.Name) then
                table.insert(own,i,true)
            else
                table.insert(own,i,false)
            end
        end
        toolStore:SetAsync("-tools"..player.UserId,own)
    end)
end)

game.Players.PlayerAdded:Connect(function(player)
    local success,value = pcall(function()
        local tools = toolStore:GetAsync("-tools"..player.UserId)
    end)
    if value ~= nil then
        local own = value
        for i,v in pairs(own) do
            if v == true then
                local tool = game.ServerStorage:GetChildren()[i]:Clone()
                tool.Parent = player.Backpack
            end
        end
    end
end)

This is all 1 script. I am trying to store which weapons a user leaves the game with, so that when they re-enter the game, they have those same weapons.

Currently, it seems to save just fine. I have tried saving a string, and it ends up returning an empty table when I use GetAsync.

Why does this not work, or what would I need to do to make this work?

If you have any further questions, please ask them. I really need to get this fixed.

0
It seems that Line 10 is giving you errors as the character is no longer in the game when it runs. What I recommend that you do is remove the pcall and run the code through until you get it to function then add it in afterwords. deth836231 142 — 4y
1
I am going to change my entire outlook on how I save the tools. Thank you though. appxritixn 2235 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

As deth836231 said, the character is not in the game anymore.

Instead make it so when people buy/get the item, make a table where it's true that they own the thing:

local DATASTORESERVICE = game:GetService("DataStoreService")
local toolStore = DATASTORESERVICE:GetDataStore("-tools")

-- Change to where ever the remote is
local gotToolRemote = game.ReplicatedStorage.GotTool

local TOOL_TABLE = {
    ["Random tool"] = false; -- etc.
}

-- When player buys/get's a tool:
gotToolRemote.OnServerEvent:Connect(function(player, tool) -- This fires with the 'tool' varible as the tool in the players backpack
    -- Now we save the tool:
    local success, value = pcall(function()
        local toolsOwned = toolStore:GetAsync(player.UserId)
    end)
    if value ~= nil then
        toolsOwned[tool.Name] = true
        toolStore:SetAsync(player.UserId, toolsOwned)
    end
end)
-- Player added:
game.Players.PlayerAdded:Connect(function()
    local success,value = pcall(function()
        local tools = toolStore:GetAsync("-tools"..player.UserId)
    end)
    if value ~= nil then
        local own = value
        for i,v in pairs(own) do
            if v == true then
                local tool = game.ServerStorage:GetChildren()[i]:Clone()
                tool.Parent = player.Backpack
            end
        end
    end
end)
0
I honestly don't know why I was doing it this way. Your way makes 100% more sense. Thanks appxritixn 2235 — 4y
0
Thanks. tobiO0310 58 — 4y
Ad

Answer this question