I’m trying to decode a players quests so I can save it since it contains something that’s not a valid utf-8 character. I’m not exactly sure what is going on here.
Saving what quests equals as a string:
local encoded2 = game.HttpService:JSONEncode(playerdata[player.UserId]["Quests"])
playerdata[player.UserId]["Quests"] = encoded2
Converting what I saved back to a table:
local decoded1 = game.HttpService:JSONDecode(playerdata[player.UserId]["Quests"])
playerdata[player.UserId]["Quests"] = decoded1
This is super odd. It seems like it’s trying to escape an already escaped string. Could you be JSONEncoding the same thing (and then its result) multiple times, by any chance?
Getting a UTF-8 error when storing the table might also be a problem, perhaps you’re storing something that can’t be stored, like userdata (Vector3, CFrame ect.) or objects like parts. JSONening would prevent the error, but when decoding the objects you might encounter problems.
Using JSON with DataStores, unless you are applying compression strategies that require the JSON format to turn a table into a string, is completely unnecessary. This work is already done internally because to be able to save data, the key and value need to be coerced into a string format.
It’s much more straight forward if you leave this work to the backend so that you can just raw-fetch and save tables back and forth through a DataStore instead of having to also involve encoding. It’s a weird gateway that just isn’t necessary to go through.
This is 100% true, but I had already created a system that I didn’t plan on saving. Rather than redoing the entire thing, I decided to save it using JSON. Rewriting the entire thing for me would take hours, though you have a very good point.
This isn’t really relevant to the problem but I can explain what’s going on here.
The decoded1 variable is a reference to the value returned from JSONDecode with the value of the index playerdata[num][“Quests”] passed as an argument. This is just creating a reference to a value. The line below sets the value at the aforementioned index to this returned value.
It’s not setting it to itself. For example, it’d be like doing this:
local a = 1
local b
b = a
a = 2
print(a) -- 2
print(b) -- 1