Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.