No, I'm not asking for youtube videos or roblox wiki links. I want a very simple explanation on how it works. An example was when someone told me how to use remote events: set what happens when the event fires, and fire the event.
Can someone tell me how do I use tables for data saving?
2 answers
Roblox doesn't save tables in Datastore. It only saves numbers and strings(text).. however, you can encode tables into strings to save in datastore, and then decode them back to a table when you read them from Datastore..
you can make your own encoder/decoder, but the HttpService has functions that encode tables into JSON formatted strings, and from JSON formatted strings into Lua tables..
the 2 functions are HttpService:JSONEncode() and HttpService:JSONDecode()
so here is a basic example:
local HttpService = game:GetService("HttpService");
local DataStoreService = game:GetService("DataStoreService");
local DataStore = DataStoreService:GetDataStore("name of data")
local data = {
coins = 200,
swords = {
"Katana",
"Galactic Saber",
"Special sword"
},
guns = {
"MP3",
".5 Caliber Sniper",
"M6 machine gun"
}
}
local stringData = HttpService:JSONEncode(data);
DataStore:SetAsync("key goes here", stringData)
The above should save everything in that table to datastore, the following should read the data and turn it back into a table;
local stringData = DataStore:GetAsync("key goes here") or "{}"
local data = HttpService:JSONDecode(stringData); --back into a table
print(data.guns[1]) -- will print "MP3"
make sure tables are not mixed when using
JSONEncode and JSONDecode
which means table shouldn't look like this:
local t = {name = "jack", 1,2,3,4};
instead it should look like this:
local t = {name = "jack", numbers = {1,2,3,4}}
6 comments
Okay, can you explain also what do the parameters work? Like the data and numbers part, what defiones what?
AHHHHHHHHH YOU TOOK MY AWARD
what do you mean the data and numbers part?
You said "local data = ..." and sorry, it was "local guns = ..." not numbers.
i don't understand
nvm then
First of all create a server script in serverscriptservice Then turn API services on. So the server knows where to store the data via.
You need to store the data as two variables like this Datastore does not fully function in ROBLOX studio, so do it on ROBLOX itself
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local PlayerUserId = "Player_"..player.UserId
local Data
local sucess, errormessage = pcall(function()
local Data = myDataStore:GetAsync(PlayerUserId)
end)
if sucess then
Deaths.Value = Data -- Storing variable
Wins.Value = Data -- Storing variable
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserId = "Player_"..player.UserId
local Data = {Deaths = player.leaderstats.Deaths.Value, Wins = player.leaderstats.Wins.Value;}
-- We stored the data to two variables above this comment
local sucess, errormessage = pcall(function()
myDataStore:SetAsync(PlayerUserId, Data)
end)
if sucess then
print("Data succesfully saved")
else
print("Data not saved")
warn(errormessage)
end
end)
Please comment if there are any errors
3 comments
ur answer is also great.. don't worry
here is a free up vote
Aww, thanks dude. I appreciate that :) it was just a joke. But then you actually upvoted mine. Thats nice of you :D
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
like saving data in Datastore or just holding values that get destroyed when the game is not running?
Saving data in the datastore.
okay, lemme write up an answer