Table saves index as a string/dictionary instead of as a number index

I have a table that basically looks like this

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)

1 Like

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.

1 Like

Yeah uh that is quite literally verbatim what i said

1 Like

Works in raw lua
Might just be your “i”

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)

3 Likes

Used tonumber, didnt work, and i cant be a string anyway since the i comes from a for loop

The only processing the table goes through is datastores and :getasync()

Datastores use JSON encoding by default

2 Likes

DataStores go through JSONEncode before hitting their servers. You can check what JSON value Roblox has from the DataStore Manager on Creator Hub.

1 Like

So the question is

  1. Is there a way to save the value of my table so that when it goes through json encoding, it saves as a number and not a string
  2. If not then how do i work around this
  1. No, as @metatablecatmaid said, you cannot have a table-dictionary mix in JSON
  2. 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).

You can convert keys back to numbers using tonumber.

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.