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
5 years ago

First, my code:

01local DATASTORESERVICE = game:GetService("DataStoreService")
02local toolStore = DATASTORESERVICE:GetDataStore("-tools")
03game.Players.PlayerRemoving:Connect(function(player)
04    pcall(function()
05        local tools = {}
06        for _,v in pairs(player.Backpack:GetChildren()) do
07            table.insert(tools,1,v)
08            print("Saving: "..v.Name)
09        end
10        for _,v in pairs(player.Character:GetChildren()) do
11            if v:IsA("Tool") then
12                table.insert(tools,1,v)
13                print("Saving: "..v.Name)
14                break
15            end
View all 42 lines...

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 — 5y
1
I am going to change my entire outlook on how I save the tools. Thank you though. appxritixn 2235 — 5y

1 answer

Log in to vote
1
Answered by 5 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:

01local DATASTORESERVICE = game:GetService("DataStoreService")
02local toolStore = DATASTORESERVICE:GetDataStore("-tools")
03 
04-- Change to where ever the remote is
05local gotToolRemote = game.ReplicatedStorage.GotTool
06 
07local TOOL_TABLE = {
08    ["Random tool"] = false; -- etc.
09}
10 
11-- When player buys/get's a tool:
12gotToolRemote.OnServerEvent:Connect(function(player, tool) -- This fires with the 'tool' varible as the tool in the players backpack
13    -- Now we save the tool:
14    local success, value = pcall(function()
15        local toolsOwned = toolStore:GetAsync(player.UserId)
View all 36 lines...
0
I honestly don't know why I was doing it this way. Your way makes 100% more sense. Thanks appxritixn 2235 — 5y
0
Thanks. tobiO0310 58 — 5y
Ad

Answer this question