Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Can someone tell me how do I use tables for data saving?

Asked by 4 years ago

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.

0
like saving data in Datastore or just holding values that get destroyed when the game is not running? User#23252 26 — 4y
0
Saving data in the datastore. User#32819 0 — 4y
0
okay, lemme write up an answer User#23252 26 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

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}}

0
Okay, can you explain also what do the parameters work? Like the data and numbers part, what defiones what? User#32819 0 — 4y
0
AHHHHHHHHH YOU TOOK MY AWARD maxpax2009 340 — 4y
0
what do you mean the data and numbers part? User#23252 26 — 4y
0
You said "local data = ..." and sorry, it was "local guns = ..." not numbers. User#32819 0 — 4y
View all comments (2 more)
0
i don't understand User#23252 26 — 4y
0
nvm then User#32819 0 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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

0
ur answer is also great.. don't worry User#23252 26 — 4y
0
here is a free up vote User#23252 26 — 4y
0
Aww, thanks dude. I appreciate that :) it was just a joke. But then you actually upvoted mine. Thats nice of you :D maxpax2009 340 — 4y

Answer this question