#How do I save a table using DataStore?

1 messages · Page 1 of 1 (latest)

primal cliff
#

I'm really new to using datastores, and currently I'm trying to make an "inventory" type system. Here's the code
I don't know why I'm using a leaderstats script for this but I had to start somewhere, and no it's not AI i grabbed it from toolbox (as I said I'm new to datastores)

#

bruh

#

i cant send script

#
local DataStore = game:GetService("DataStoreService")
local ds3 = DataStore:GetDataStore("SaveData3")
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local inv = {}
    wait()
    local plrkey = "id_"..player.userId
    local GetSaved3 = ds3:GetAsync(plrkey)
    if GetSaved3 then
        inv = GetSaved3[1]
        print(GetSaved3)
    end
    while true do
        wait(10)
        local setSuccess, errorMessage = pcall(function()
            
            ds3:SetAsync(plrkey,{inv})
        end)
        if not setSuccess then
                warn(errorMessage)
        end
    end
end)

--Saves the data when the player exits the program.
game.Players.PlayerRemoving:Connect(function(plr)
    
    ds3:SetAsync("id_"..plr.UserId, {inv})
end)
#

the problem is that well since I can't put tables into stringvalues, I had to just make a table

#

but I cant make it save when you leave because... the value isnt stored anywhere'

hallow goblet
drowsy kindle
#

change ds3:SetAsync(plrkey,{inv}) to

#

ds3:SetAsync(plrkey,inv)

#

there is lots of other changes that could be made but thats whats making it not work

#

the data should not be set when the player joins only when they leave or server shuts down

drowsy kindle
hallow goblet
drowsy kindle
hallow goblet
drowsy kindle
hallow goblet
fervent otter
fervent otter
drowsy kindle
#

Explain it

fervent otter
#

the problem is between here lua game.Players.PlayerAdded:Connect(function(player) ... local inv = {} ... if GetSaved3 then -- no accounting for nil data (first load) inv = GetSaved3[1] print(GetSaved3) end
and here lua game.Players.PlayerRemoving:Connect(function(plr) --inv is nil, thus GetSaved3[1] is nil when it loads again later ds3:SetAsync("id_"..plr.UserId, {inv}) end)
quite trivial really 🧐

drowsy kindle
#

You dont seem to understand

fervent otter
#

there's an interesting problem here too ```lua
while true do
wait(10)
local setSuccess, errorMessage = pcall(function()

        ds3:SetAsync(plrkey,{inv})
    end)
    if not setSuccess then
            warn(errorMessage)
    end
end```

where this will continue to setasync on the playerkey even after the player disconnects, but this would still return {{}} as a result on the next getasync, so it is not the primary problem

fervent otter
drowsy kindle
#

This user stated they were beginner, so I wanted to ensure that they aren't saving the inventory table as a table within a table.

#

That could easily lead to issues retrieving data for a beginner if it leads to confusion.

#

Also, he accidentally overwrites the data each time it is loaded.

fervent otter
drowsy kindle
#

I dont disagree with your solutions, but I know for a fact my suggestions were of sound logic.

fervent otter
fervent otter
drowsy kindle
#

okay that makes sense, I still think they should use a variable like local storedData = {inv} to avoid confusion

drowsy kindle
fervent otter
# drowsy kindle okay that makes sense, I still think they should use a variable like ``local sto...

imo the bigger concern is not managing to find or understand the docs page on datastores https://create.roblox.com/docs/cloud-services/data-stores and somehow expecting to make an inventory system afterthefact. maybe it's a lack of turning on roblox api services too, still... s1 bar is loooow bro

How to implement data stores to store persistent data.

primal cliff
#

since when was roblox studio community this toxic...
the toolbox exists for a reason, and yes, i understand the code, I was just lazy to write it all myself

primal cliff
fervent otter
primal cliff
#

but thats the problem, I'm wondering if I should ditch the leaderstats playeradded script and just copy the datastores to a new script

primal cliff
#

and btw why I was using a toolbox leaderstats script was because ahem im lazy and i just copied and pasted my old script to this one

primal cliff
#

true...

#

my laziness doesnt benefit me most of the time

#

🤷

#

so here's what im thinking

#

I need to make a variable

#

that is accessible from all parts of the script

#

and each player needs one of these variables tied to their plrkey

#

but since I can't use values to store the table

#

technically though

#

if the "inv" is already tied to the plrkey

#

it should be able to save

#

dang I really need more experience in datastores

primal cliff
#

and theres another problem

#

when I'm adding things to the inv

#

its not accessible

#

so...

primal cliff
fervent otter
# primal cliff when I'm adding things to the inv

local playersdata={} ... playeradded(plr) local t={} playersdata[plr]=t t.money=load_datastore_etc end ... playerremoving(plr) savedatastore(playersdata[plr]) playersdata[plr]=nil end end dont forget game:bindtoclose

#

oh and dont expect that code to work. i can't do all of it for you fingerguns

primal cliff
#

just one last thing i leave the playerdata OUTSIDE of the playeradded?