Okay, I understand datastore, but I heard that you needed JSON or HttpService to do this, how to use JSON?
Thanks in adnvace
Okay, I understand datastore, but I heard that you needed JSON or HttpService to do this, how to use JSON?
Thanks in adnvace
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.
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:
local PlayerData = {
Player = {Coins = 0, Level = 0, Diamonds = 0, Kills = 0};
}
game.Players.PlayerAdded:Connect(function(plr)
local key = "User_"..plr.UserId
end)
game.Players.PlayerRemoving:Connect(function(plr)
local key = "User_".plr.UserId
end)
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.)
-- Inside of player added.
if Datastore:GetAsync(key) ~= nil then
-- assign data
else
-- load prev data
end
Then you'd want to assign the table to the player:
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..):
local suc, msg = pcall(function()
wait(5)
PlayerData[Player] = HttpService:JSONDecode(DataStore:GetAsync(Key));
end)
if not suc then
Player:Kick(msg)
end
Whenever the player IS leaving, you'd have something like (remember I am wrapping in pcall for any errors.):
local success, message = pcall(function()
if PlayerData[Player] ~= nil then
DataStore:SetAsync(key, HttpService:JSONEncode(PlayerData[Player]))
print('Player Data set.')
end
end)
if not success then
print('Error: '..message)
end
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!
This discussion is preserved as it was. New answers, comments, and votes are closed.