Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
|
| 09 Apr 2014 10:21 PM |
I have no idea how to work around these at all; I have tried but I doubt it works. I also thought what the wiki had was veg, which didn't help at all. Anyways here's my attempt; though I am missing lines as you may notice. Can anyone show me an example or at least explain what I'm doing wrong?
local u = assert(LoadLibrary("RbxUtility"))
game.Players.PlayerAdded:connect(function(p) local model = Instance.new('Model', p) model.Name = 'Stats' local rep = Instance.new('IntValue', model) rep.Name = 'Reputation' rep.Value = 1 local repxp = Instance.new('IntValue', model) repxp.Name = 'RExp' repxp.Value = 0 local cash = Instance.new('IntValue', model) cash.Name = 'Cash' cash.Value = 0 local class = Instance.new('Model', model) class.Name = 'Classes' p:WaitForDataReady() --[[ local repv = p:LoadNumber('Rep') local cashv = p:LoadNumber('Cash') local rexp = p:LoadNumber('RExp') if repv>1 then rep.Value = repv end if cashv>0 then cash.Value = cashv end repxp.Value = rexp ]]-- -- Test -- local data = u.DecodeJSON(p:LoadString('Data')) -- I have no clue what to do end)
game.Players.PlayerRemoving:connect(function(p) --[[ p:SaveNumber('Rep', p.Stats.Reputation.Value) p:SaveNumber('Cash', p.Stats.Cash.Value) p:SaveNumber('RExp', p.Stats.RExp.Value) ]]-- -- Test -- local stats = {} for _, v in pairs(p.Stats:GetChildren()) do table.insert(stats, v) end local data = u.EncodeJSON(stats) p:SaveString('Data', data) end) |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Apr 2014 10:41 PM |
EncodeJSON basically converts a table to a string {a = 5, b = 5} -> "{'a': '5', 'b': 5'}" or something like that And DecodeJSON takes ^ and makes it into {a = 5, b= 5} |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
|
| 09 Apr 2014 10:43 PM |
| Uh... Can I get a template of this? I seriously have no idea how to use it. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Apr 2014 10:49 PM |
en.wikipedia.org/wiki/JSON http://wiki.roblox.com/index.php/EncodeJSON_(Function) http://wiki.roblox.com/index.php/DecodeJSON_(Function)
Example (Not tested but should work assuming I spelled everything correctly):
local RBXUtility = LoadLibrary("RbxUtility") local encodeJSON = RBXUtility.EncodeJSON local decodeJSON = RBXUtility.DecodeJSON
local myJSON = encodeJSON({a=5, 1, b=7}) print(myJSON) local myTableFromMyJSON = decodeJSON(myJSON) print(unpack(myTableFromMyJSON)) |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
|
| 09 Apr 2014 10:52 PM |
I have a question, when it comes to decoding, does it return a table from the its variable? For say;
-- According to cntkillme's
local list = decodeJSON(myJSON)
for _, v in pairs(list) do print(v) end |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
| |
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Apr 2014 10:53 PM |
| Yes, decodeJSON returns a table (that's sort of the point) |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
|
| 09 Apr 2014 10:54 PM |
| Ok thanks, that makes more sense. |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
|
| 09 Apr 2014 10:56 PM |
Wait hold on, let's say I'm saving stats in a model and in that stats, there's int values and another model. (I'm saving the stats that are in the model)
Am I able to load what's inside this model once the player enters again? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Apr 2014 11:00 PM |
Sure, just create values and load from them. Something like:
local values = {cash = 5, maxHealth = 10}
--create stats --load stats if exist (set the variable values to the decoded JSON, otherwise just ^) --update values table on stats change (Changed event) --save stats as JSON (EncodeJSON) |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
|
| 09 Apr 2014 11:08 PM |
| How about instances? Like model in a model. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 09 Apr 2014 11:10 PM |
Not going to work, userdata is not something you can save as a string unless you save all their properties and create it. So you can't just do:
--save json with table: {x = workspace.Part} you would have to do something like: --save json with table: {name="Part", BrickColor="blah", ...} And creating it later |
|
|
| Report Abuse |
|
|
Raphael7
|
  |
| Joined: 03 Dec 2008 |
| Total Posts: 2479 |
|
|
| 09 Apr 2014 11:11 PM |
| Sorry for the trouble and thanks for the help! |
|
|
| Report Abuse |
|
|