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

Attempting to use JSON, Confused on a few things, need help basically?

Asked by
Kitorari 266 Moderation Voter
7 years ago
Edited 7 years ago

So this is my attempt at using JSON ((Literally just wrote it here.)) I'm attempting to make a way to save data, but as you can tell, I do not have a very good grasp on how it works. So If someone could explain it or maybe correct how I did it, I'd be super Thankful.

PS do I use datastore in this too? if so, how? Thanks.

local Http = game:GetService("HttpService")

game.Players.PlayerAdded:connect(function(P)

local F = Instance.new("Folder",P)
F.Name = "Folder"

local CS = {
Money = P.Folder.Value,
Level = P.Folder.Value
}



for i=1,CS do

local M = Instance.new("IntValue",F)
M.Name = CS--How do I go about putting the name of the values from CS?
M.Value = CS--How do I go about putting the value of the named strings from CS?
end
end)



game.Players.PlayerRemoving:connect(function(P)

local CS = {
Money = P.Folder.Value,
Level = P.Folder.Value
}

local JSON = Http:Jsonencode(CS)
end)

1 answer

Log in to vote
2
Answered by 7 years ago

Instead of :Jsonencode(), it's :JSONEncode(). To access a variable in a table, you simply do as you would when trying to access something in workspace, and so on. Here is an example:

mytab = {s = true}
print(mytab.s)
--->true

--you can also do it this way
mytab2 = {a = 'Hello World!'}
print(mytab2['a'])

as for the above code, you can do this:

local things = {
"Money";
"Level";
}
for i = 1,#things do
local M = Instance.new("IntValue",F)
M.Name =  things[i]
M.Value = CS[things[i]]
end

Also, know that you don't have to encode the data into JSON (i dont). It's really up to you if you want to use encode data into JSON or not.

Ad

Answer this question