Seeing how many characters in a Datastore

local loadJSON = playerDataStore:GetAsync(player.UserId)
print(httpService:JSONEncode(playersData[player.UserId])) -- Error here

Error
[ Argument 1 missing or nil]

Simply just trying to see how many characters in a datastore

1 Like

You’re loading the data into “loadJSON” but use “playersData[player.UserId]” as the argument to encode the table.

Try this:

local loadJSON = playerDataStore:GetAsync(player.UserId)
print(#httpService:JSONEncode(loadJSON)) -- Added # to get the string length.

[Can't convert to JSON]

Make sure whatever you’re converting to JSON is an array.
It specifies this in the page for :JSONEncode()
It will not convert, Strings, Bools etc

Someone else had the same issue, apparently it might have something to do with the max table size it can encode:

JSONEncode can definitely encode strings and bools. It can also handle dictionaries, too, not just arrays. It can’t encode Roblox-exclusive types, such as Vector3s and Color3s, though.

You can’t store userdata types (like Vector3 and Color3) into datastores anyways, and since he’s directly reading from a datastore it shouldn’t include any for those to be passed into JSONEncode.

I was referring to Encoding a String or Bool alone. That’s not in an array. Which gives the Can’t Convert to JSON Error.

The post you linked is from 2014. I doubt it’d have something to do with the size. As I’m easily able to convert a large table into a 30602 character long Json table.

1 Like

It should be a table, as he’s passing it on directly from the GetAsync function. The only way it may be any other variable type is if the entry doesn’t exist and it returns nil from GetAsync.

Meaning that playerDataStore:GetAsync(player.UserId) returns nil, and since JSONEncode wouldn’t be able to encode nil it’d throw the same error I’m pretty sure. So the issue could be a matter of the key not existing in the datastore.

Why not try printing the raw value that’s returned from GetAsync first before making the JSON call? The error is relatively straight forward, so it may have to do with what the DataStore is returning.

  {"Kills":0,"Level":1,"Games":0,"Wins":0,"EquippedClass":"Knight","Classes":{"Scout":{"Owned":false,"Equipped":{"Weapon":"Dagger","Armour":"Furious Finn","Trail":"None"},"Weapons":["Dagger"],"ID":3,"Armours":["Furious Finn"],"Trails":["None"]},"Knight":{"Owned":true,"Equipped":{"Weapon":"Classic Sword","Armour":"Lord Griswold","Trail":"None"},"Weapons":["Classic Sword"],"ID":1,"Armours":["Lord Griswold"],"Trails":["None"]},"Archer":{"Owned":true,"Equipped":{"Weapon":"Bow","Armour":"Iron Armour","Trail":"None"},"Weapons":["Bow"],"ID":2,"Armours":["Iron Armour"],"Trails":["None"]},"Hunter":{"Owned":false,"Equipped":{"Weapon":"Crossbow","Armour":"Bombo","Trail":"None"},"Weapons":["Crossbow"],"ID":4,"Armours":["Bombo"],"Trails":["None"]}},"Losses":0,"Gold":100000,"Gems":0,"Exp":0,"Deaths":0}

this is what print(loadJSON) returns

print(tostring(loadJSON))

I wonder if this would work?

Ye, printed the same thing

1 Like

Sorry, my bad.

print(tostring(loadJSON):len())

Forgot that you’re checking for string length and forgot to throw the len operator in there. Does that work?

Returned 797, so is that the amount of characters then?

Yep.

print((“ya”):len()) should give you 2.
So then, using that logic, the number returned from using string.len on a tostring of the JSON data should be the string length.

1 Like

It looks like the saved value is already a string. tostring and JSONEncode won’t be needed. You could simply print the length just by doing print(#loadJSON)

@NinjoOnline In the future, you should make sure you know what the value itself is if you’re getting any errors related to the arguments.

1 Like

That’s only assuming if the data returned from a GetAsync is a JSON string, which my method is intended to be a catch-all-ish, minus the encoding if not already done so. That can be done by checking typeof(returnedData) for a table value, encoding and printing the length.