WHAT is happening when I set a table[key] to nil?

Hello everyone,

I would like to share an issue I encountered while testing my game related to pet data on the client side. After a thorough investigation, I noticed inconsistencies in the results when I set pets[index] to nil, the pets table sometimes changes after my for-loop.

Firstly, I have noticed that passing a table through a remote event will perform like this:

local a = {
    [1] = 1,
    [2] = nil,
    [3] = nil,
    [4] = 4
}

remoteEvent:FireClient(player, a)

--[[ What we receive on the client is exactly the same as `a` ]]--

local b = {
    [1] = 1,
    [4] = 4
}

remoteEvent:FireClient(player, b)

--[[ on client is
    b = {
        [1] = 1
    }

    b[4] is gone
]]--

Here is the code I used:
image

In some cases, the code executes correctly, as shown in the following image:


However, in other cases, it produces incorrect results. When I pass the modified table to the client, all data after the first nil value is lost. This can be seen in the following images:

image

I am puzzled as to why the same operation produces different results. Could someone provide insight into this issue? Any help would be greatly appreciated.

Thank you!

table.remove should solve your issue.

table.remove(array, index)

I believe arrays cut every value after there is a gap between indexes.

Just wanted to give supporting information from Roblox Staff about this exact edge case