Um.. yes. I Love that spelling of yours.
Anyways... I don't know much on this, but here are the links, I can also provide an example or of something that I know.
JSON Format
JSONEncode()
function transforms a Lua table into a JSON object or array and CANNOT ncode anything other than strings, numbers, tables, and booleans, anything else would error.
JSONDecode()
function transforms a JSON object or array into a Lua table.
HttpService()
allows HTTP requests to be sent from game servers.
Anyways, i'll show you an example I have, assuming that you already know what DatastoreService
is.
We already have everything set up, and it should look something like:
02 | Player = { Coins = 0 , Level = 0 , Diamonds = 0 , Kills = 0 } ; |
05 | game.Players.PlayerAdded:Connect( function (plr) |
06 | local key = "User_" ..plr.UserId |
10 | game.Players.PlayerRemoving:Connect( function (plr) |
11 | local key = "User_" .plr.UserId |
Okay so, what I would do is check to see if player has any existing data, if not then load the data they had previously. (checking to see if they have a prev key.)
2 | if Datastore:GetAsync(key) ~ = nil then |
Then you'd want to assign the table to the player:
1 | PlayerData [ plr ] = PlayerData |
and load previous data which we'll be wrapping in pcall, to find any errors (I usually kick to prevent overlapping of data/loss of it, but you can return it instead..):
1 | local suc, msg = pcall ( function () |
3 | PlayerData [ Player ] = HttpService:JSONDecode(DataStore:GetAsync(Key)); |
Whenever the player IS leaving, you'd have something like (remember I am wrapping in pcall for any errors.):
1 | local success, message = pcall ( function () |
2 | if PlayerData [ Player ] ~ = nil then |
3 | DataStore:SetAsync(key, HttpService:JSONEncode(PlayerData [ Player ] )) |
4 | print ( 'Player Data set.' ) |
8 | print ( 'Error: ' ..message) |
I suggest reading what I had above before even using my code, as I didnt explain the proces of it. Hoping that you'd just 'understand' what were getting here (also not going to lie, im bad at explaining things too.)
Welp, hope this helped. Have a nice day!