#How do I save a table using DataStore?
1 messages · Page 1 of 1 (latest)
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'
Ain’t no use askin’ no more, cowboy.
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
THIS IS A HELP CHAT NOT A MAKING FUN OF PEOPLE CHAT !!!
DERANK
How am I making fun of him?
read your comment please
read your comment to me please
why u cant read?
I can read fine.
so you need help fixing toolbox code? bruh, at that point just write it yourself. what did you submit for s1 rank?
this is incorrect, what you suggest would actually break it further, not fix it, in addition to not fixing the first problem.
Explain it
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 🧐
You dont seem to understand
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
that'd be you, friend 
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.
indeed, and they went on to say I don't know why I'm using a leaderstats script for this but I had to start somewhere so i'm assuming their goal is an inventory script, but they don't know how to do datastores so their use of inv as an empty table in a table is meant to be a stand-in for their data
I dont disagree with your solutions, but I know for a fact my suggestions were of sound logic.
and this is where your mistake is:
so their use of inv as an empty table in a table is meant to be a stand-in for their data
makes sense
at this point given OP's clear lack of understanding of basic api usage required for s1 rank, i question how/what did OP's s1 come from ;p
okay that makes sense, I still think they should use a variable like local storedData = {inv} to avoid confusion
idk the ranks are silly at this point
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
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
I think the problem is that the "inv" table isnt stored anywhere because I CAN'T store it anywhere (doesn't go into stringvalues or anything) so I can't access it because Inv is just a table in a script, not even an instance
and since inv was declared in playeradded the code cannot find it in playeremoving
yes, you have local inv in playeradded. the local scope means it isn't visible to the playerremoving function
yeah
but thats the problem, I'm wondering if I should ditch the leaderstats playeradded script and just copy the datastores to a new script
that was my first suggestion 
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
reap what you sow 
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
and just so you know what I submitted for s1 was a bunch of attack systems
I don't learn things the orthodox way, I kinda just learn as I go, so there's a bunch of necessary functions I have no clue about
and theres another problem
when I'm adding things to the inv
its not accessible
so...
m1 go brrrr 
nah i kinda suck at animating too I just made some stuff like lightning strikes and stuff
honestly back then I'm not sure how I made it to s1
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 
eh i get the logic thx ill see what i can do
just one last thing i leave the playerdata OUTSIDE of the playeradded?