local checkredeems = {
[“Normal”] = {},
[“Premium”] = {}
}
And another line
checkredeems[redeemtype] (normal or premium) [i] = true
Now the problem is, if instead of setting the values in ascending order (like first setting checkredeems[redeemtype][1] then [2] then [3]) you set random values (like maybe start with [3] first, then [5], then [2])
then the index saves as a string for some reason and the whole thing breaks because checking for “redeemdata[rewardtype][2]” returns nil since it expects [2] not [“2”]
What do i do exactly about this im baffled
(screenshot of me setting the values in ascending order starting from 1)
Assigning in order created a table, wherein the elements can be accessed via their index i.e. a number
Assigning randomly created a dictionary, wherein the data can be accessed via their key i.e. the string.
So, the problem is you can’t access it using a number, because it’s setting it to a string?
if so, then you’re setting it wrong, and [i] is actually a string, use tonumber().
and if you want it in order, use table library. (if this is also a problem, I couldn’t really understand what you were trying to say)
table.insert, table.remove, table.find
If you’re using JSONEncode (or a lot of other Roblox things that process tables, since from what I’ve seen, most Roblox APIs that cross the VM state boundary, encode a table as JSON, then decode it on the other side), unless the table explicitly has a list with ordered keys (from 1… onwards), the JSON encoder assumes its a string key.
This is obviously because JSON doesn’t let you define numeric keys in an object.
{
3: "a" // this is invalid syntax
}
And you also cant have mixed tables in JSON, you can only have or the other, maybe the encoder should really do this:
[null, null, item]
But this feels very niche (and could have dragons with performance), so the JSON encoder would rather just encode 3 as "3", since its calling tostring on the index, to get a string-encodable value, and the process is non-reversable (since “3” is very much a valid key)
No, as @metatablecatmaid said, you cannot have a table-dictionary mix in JSON
I’d just choose dictionaries, setting and retrieving data using i as a string key. I couldn’t be bothered to find a workaround when that works fine so do tell if you stick with it and find a solution
Can i see the entire block of code? I think it may be something wrong with your “i” variable or smth. Otherwise i dont rlly know why it might be doing this especially if tonumber() isnt working
Sparse arrays (i.e. arrays with holes/gaps) can’t be encoded to JSON (which is what happens when saving to datastore), so it converts it to a dictionary (string keys).
Long story short just ended up adding an additional line where it fills in the empty gaps with false
(ex: if i have redeemcheck[5] = true then it’ll fill redeemcheck[1] to [4] with false since they’re supposed to be false by default anyways and that way it also fills the arrays with gaps problem)
Thanks for the replies