i dont understand why this is happening
maybe try a remoteFunction?
I’m slow just realized it didn’t really fix the issue… my bad
I think I have an explanation to why Roblox is only printing out keys 1 and 2 of the table sent by the server. So basically, Roblox sees it as an array and converts the table to arrays with numeric keys and converts the data within the table one by one - increasing by one each time. However, you are missing keys 3, 4, 6, and 7 so Roblox stops as key 3 is not there so it prints out the table with just keys 1 and 2 of the table. Hopefully, that somewhat makes sense to you.
Another solution you could do is making the table a dictionary to make Roblox print out the table in full. Therefore the keys would most likely need to be strings. I have tested this and it does work.
This appears to be a bug in the engine as opposed to an intended feature as the behaviour is very inconsistent and none reflective of other programming languages, I would suggest making a bug report in #bug-reports:engine-bugs and sending in the file.
The behaviour is most likely an error in the optimisation, which is not something you can fix.
The data you sent is an array, and as @oplkel pointed out, you are missing several indexes. By default, every index is an increment of 1, and when it reaches an index that doesn’t exist like Data[3], it returns nil
. Roblox uses ipairs
to iterate over arrays, but ipairs
stops when it encounters the first nil
value.
To avoid this issue, you can use pairs
, which works for all table types, including those with non-sequential keys:
for key, value in pairs(Data) do
print(key, value)
end
This way, pairs
will iterate through all the elements in the table, even if some indices are missing or there are nil
values. This is not a bug but infact an intended feature.
It seems it does not exactly work as you intended but I could see the theory behind it. Here is my code for it:
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(Data)
for key, value in pairs(Data) do
print(key, value)
end
end)
The basic problem is that when the remote event copies the data from the table to send, it is misreading it. It is seeing numeric keys and attempting to iterate over them stopping at the first nil key and nil value.
If you declare the missing keys as nil (e.g. [3] = nil
etc), to ‘fill’ the holes in the array you can trick the remote into sending the entire array, so long as no nil values were assigned above the value of table.maxn(data)
.
There is a warning in the guide on remotes to suggest not sending nil values, or mixed keys. This is why. As the table is reinterpreted in each run context as either an array or a dictionary it can lead to inconsistent results.
For example, sending a holey array with 1 extra nil value assigned on the end truncates the table at the index before the last hole, cutting off data. But with two nil values on the end it sends the entire array… and I’m not sure this behaviour is even that consistent.
tldr; Either, use tables with sequential numeric keys, or use a dictionary with non-numeric keys.
The simplest explanation is that seemingly JSON encoding (for arrays) is nil-terminated, which means that the encoder will encode the pairs of the table until it hits a nil key-value pair. I strongly oppose the idea the that is at all an intentional feature.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.