I recently started using ‘JSONEncode’ and ‘JSONDecode’ in my game for the player’s data. Out of thousands of players, I’ve only had a couple report that when opening the game, nothing loads in and they get errors for things being strings when they shouldn’t be, because their JSON failed to decode their data, and then when leaving, the data that wasn’t decoded gets encoded AGAIN if that makes sense. This means that even if the ‘JSONDecode’ doesn’t fail the next time they join, the data will need to be decoded a second time (i think). I don’t know what else would cause the issue.
The only way I’ve been able to replicate the errors that those few players are having is by messing around with my ‘JSONDecode’ function and getting the cant’ parse JSON error, then rejoining.
Here’s where the data is encoded every time the player leaves:
local function encodeData(data) -- I use profile service, and the 'data' here is just the player's profile.Data
local totalLength = 0
for i, value in pairs(data) do
data[i] = httpService:JSONEncode(value)
end
return data
end
I tried just encoding the player’s profile.Data
but that causes errors with ProfileService
so instead I had to loop through each data point within profile.Data
.
And here’s where it’s decoded right after their profile loads:
for i, dataPoint in pairs(profile.Data) do
if dataPoint == nil then continue end
if type(dataPoint) ~= "string" then continue end
local decoded = httpService:JSONDecode(dataPoint)
profile.Data[i] = decoded
end
I’m pretty inexperienced when it comes to JSON so I’m not sure why this would ever through up the can’t parse JSON error for some players.
I’d also like to know if there’s anything I can do to fix player’s data that got encoded twice.