Cant save Tables to DataStore

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? i am trying to save a table to datastore

  2. What is the issue? I can’t register a table to datastore

  3. What solutions have you tried so far? I looked at the topics created by others in the devforum, but I still couldn’t find the solution.
    Although I open the API services from the studio settings, it does not work and I do not get any output errors.
    here is the script (In ServerScriptService and its a regular script)

local pl,wk,dt = game:GetService("Players"),game:GetService("Workspace"),game:GetService("DataStoreService")
local ourdt = dt:GetDataStore("OurData")
function pcc(p)
	local leaderstats = Instance.new("Folder",p)
	leaderstats.Name = "leaderstats"
	local wins = Instance.new("IntValue",leaderstats)
	wins.Name = "Wins"
	local points = Instance.new("IntValue",leaderstats)
	points.Name = "Points"
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	coins.Value = 100
	local data = {["Wins"] = wins.Value,["Coins"] = coins.Value}
	local sc,ms = pcall(function()
		ourdt:GetAsync(p.UserId,data)
	end)
	if sc then
	else
		print("Datastorda Load hatası var") -- "There is an error at Datastore Getting"
	end
end
function pgg(p)
	local data = {["Wins"] = p:WaitForChild("leaderstats"):WaitForChild("Wins").Value ,["Coins"] = p:WaitForChild("leaderstats"):WaitForChild("Coins").Value}
	local sc,ms = pcall(function()
		ourdt:SetAsync(p.UserId,data)
	end)
	if sc then
	else
		print("Datastorda Save Hatası var") -- "There is an error at Datastore Saving"
	end
end
pl.PlayerRemoving:Connect(pgg)
pl.PlayerAdded:Connect(pcc)
while true do
	wait(1)
	for i,v in pairs(pl:GetPlayers()) do
		v:FindFirstChild("leaderstats"):FindFirstChild("Coins").Value += 1
	end
end
1 Like

That’s true, you can’t save actual tables into datastores, but you can use JSON Decode/Encode with roblox HttpService. This will translate you table into JSON string, which you can save in your datastore.

local http = game:GetService("HttpService")

local t = {
    value1 = 10,
    value2 = 30
}

local json = http:JSONEncode(t) --lua table to json (string)

print(json) --{"value1":10,"value2":30}

local decoded = http:JSONDecode(json)

print(decoded) --[[  table: 0x3f56383d1e9d81bd = {
    ["value1"] = 10,
    ["value2"] = 30
}  ]]

You can easily implement this: use JSONEncode to translate your table into a string, then put that string into the datastore, and when you want to get your table, just decode the string in the datastore using JSONDecode, and get the returned table.

3 Likes

or you could turn the table into a string by yourself without httpservice, which is what I do because you don’t need to worry about rate limits

1 Like

i did the thing that u said but they were saying that datastore can already do this without needing JSON
didn’t work

ourdt:GetAsync(p.UserId,http:JSONEncode(data))

You are saying GetAsync instead of SetAsync. Anyway it must work, since datastores support strings

1 Like

it depends on how the table is set up

1 Like

like how i am need to save the table?
i set the tables like the people do when they need to save the tables

I think @D0RYU means that datastores support arrays, but not dictionaries. I barely remember something like that.

1 Like

in this post

they actually did

No no no don’t do this. You can save tables to datastores just fine, because Roblox already encodes it for you. Doing this actually makes your datastore less efficient.

1 Like

yeah thats what i got learned from other topics , but where is the error at my script?

Oh, i didn’t know about that, thanks for letting me know. Anyway i tried doing the same thing a long time ago, and it didn’t work. But i can’t really remember what i was doing, so i can’t say if i did it wrong or not.

1 Like

Print the message from the pcall failure. It will contain important information describing the error. Right now you are sinking that information by not using the ms variable on your pcall lines.

1 Like

image
i did like this but it still didnt print anything

image
just placed it to the success too , now it prints nil
image

A couple things

  • GetAsync only takes one parameter which is the key, so I don’t know what you are doing with data there.

  • When you retrieve your data, you then have to set the leaderstats values to the retrieved data. Example of revised script:

local pl,wk,dt = game:GetService("Players"),game:GetService("Workspace"),game:GetService("DataStoreService")
local ourdt = dt:GetDataStore("OurData")
function pcc(p)
	local leaderstats = Instance.new("Folder",p)
	leaderstats.Name = "leaderstats"
	local wins = Instance.new("IntValue",leaderstats)
	wins.Name = "Wins"
	local points = Instance.new("IntValue",leaderstats)
	points.Name = "Points"
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	coins.Value = 100

	local sc,ms = pcall(function()
		return ourdt:GetAsync(p.UserId)
	end)
	if sc then
        wins.Value = ms.Wins
        coins.Value = ms.Coins
	else
		print("Datastorda Load hatası var") -- "There is an error at Datastore Getting"
	end
end
function pgg(p)
	local data = {["Wins"] = p:WaitForChild("leaderstats"):WaitForChild("Wins").Value ,["Coins"] = p:WaitForChild("leaderstats"):WaitForChild("Coins").Value}
	local sc,ms = pcall(function()
		ourdt:SetAsync(p.UserId,data)
	end)
	if sc then
	else
		print("Datastorda Save Hatası var") -- "There is an error at Datastore Saving"
	end
end
pl.PlayerRemoving:Connect(pgg)
pl.PlayerAdded:Connect(pcc)
while true do
	wait(1)
	for i,v in pairs(pl:GetPlayers()) do
		v:FindFirstChild("leaderstats"):FindFirstChild("Coins").Value += 1
	end
end

thank yall very much thats worked! :smiley:

1 Like

For future debugging, install a datastore editor so you can see the data. In this particular case you’d see it saves fine and is successful, and it was the reading of the data that was incorrect.

Sleitnick has a great datastore editor plugin that is really useful for this as you can browse the datastore.

2 Likes

thanks for telling! il check that plugin :grin:

Tables can absolutely be saved to datastore. I do it all of the time without problems.
If you couldn’t then how would you save things like inventories and character data?

2 Likes