You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? i am trying to save a table to datastore
What is the issue? I can’t register a table to datastore
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
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.
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.
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.
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.
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
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.
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?