Half my dictionary is removed when sent through remoteevent

I have a dictionary indexed by numbers.
When sent to the client, the table THINKS that it’s indexed and thus remove number 6 and 10 (and my string index test!).

image

Here you can see my issue. It also adds a ‘void’ parameter to index 1. There was no item index 1 before.
I could convert all indexes to strings but that’s a workaround not a solution.

Anyone knows why this is happening?

3 Likes

You can’t send tables with mixed keys. Has to be all number or all “string” keys.

Mixed tables don’t make it through the client-server boundary. Same with metatables. You’ll need to either index differently or stick to one type of key.

2 Likes

“THIS IS NOT INDEXED!” was just for testing. Even when I sent without it you get the same results.

Its probably because the number indexes aren’t in order.

This is how it looks like now, still same results

They aren’t supposed to be ordered because this is not an indexed list.

You also need to make sure you don’t skip values. 1,2,3,4,6

5 is nil. #Table only returns the keys until the first nil result. This means everything after that is nil.

You’re saying I cant use numbers as index in a dictionary?
Im almost certain I’ve done it many times before…

I’m saying between 1,2,3,4,6,7,8

Everything after 4 will not be included in the table because it stops at the first nil table index. Which in this case is 5.

I find it wierd because if it stops at first nil index, it should’ve stopped at 1.
Because there is nothing at index 1.

Eitherway I get it, I’ll just be forced to send the whole list with a bunch of empty tables to make sure all indexes are sent.

You must change the array to a dictionary and then send it over.

2 Likes

Or you can use dictionaries instead of arrays.

1 Like

I’m not gonna lie, that #1 got me as well. My assumption is maybe its starting at index #2. Anywho, you’re not limited to that. You could just forget the keys and just insert values into the table and index it in the value itself.

for i,v in ipairs(tbl) do
  if v.ID == 1 then
   -- we have key one
  end
end

Note the use of ipairs here. The thing is since we’re omitting the key then the table order will always be from 1 to #tbl with no nil keys. And if you use table.insert or table.remove it will shift the entries accordingly.

If you wanted to loop through a table with mixed keys or missing keys then you could use pairs instead of ipairs.

1 Like

It works because the 1 gets changed to void.

2 Likes

Try using a remote function instead of the remote event for sending Info to the client.

Remote Functions have the same behaviour.

Yes. Not a good idea to skip numbers in a table with number keys. Lua doesn’t really care but several Roblox features break because of it.

What I do very often is convert the integer keys to strings.

5 Likes

Yeah I’ll just have to go with an indexed list instead.

Ikr! But it definitly shouldn’t.

Anyways, thanks for the help guys!

1 Like

It’s an intended feature, likely to increase performance you can read about it on the RemoteEvent documentation.

1 Like