Trouble converting a string table value into an a number

Hello, I am trying to send a table across a remote event but I am having trouble. When the player creates a new object in the table the table gets sent over fine, but If i try to change a value in the table then it gets converted into a string when I send it over. Im not sure what is causing this and Im not sure how I can fix it. I copied the code that is used to create a new object in the table which gets sent over fine but When i change the value then it doesn’t work.

local function findItemSlot(inventory, item)
	for i,v in pairs(inventory) do
		if v[item] then
			return i
		end
	end
end
local function GetFullValue(inventory, item)
	for i,v in pairs(inventory) do
		if v[item] then
			return v
		end
	end
end
local function getEmptySlot(inventory)
	for i = 1, 20 do
		if inventory[i] == nil then
			return i
		end
	end
end


-- object gets added into table
local itemType = itemModule[item]["Type"]
local playerInventory = playerInventories[plr]
playerInventory[itemType] = playerInventory[itemType] or {}
local inventoryItemType = playerInventory[itemType]
local itemExists = findItemSlot(inventoryItemType, item)
if not itemExists then
	inventoryItemType[getEmptySlot(inventoryItemType)] = {[item] = amt}
end
UpdateInv:FireClient(plr, playerInventory)

--object gets edited
local playerInventory = playerInventories[plr]
playerInventory[itemType] = playerInventory[itemType] or {}
local inventoryItemType = playerInventory[itemType]
local tempVal = GetFullValue(inventoryItemType, item1)
inventoryItemType[item1Slot] = inventoryItemType[item2Slot]
inventoryItemType[item2Slot] = tempVal
UpdateInv:FireClient(plr, playerInventory)

client:

UpdateInv.OnClientEvent:Connect(function(playerInv)
	playerInventory = playerInv
end)

item Added:
image

Item Edited:
image

using RemoteEvent/RemoteFunction, to send a table with numeric indexes, it must fulfill the condition of an array (consecutive numeric indexes starting at 1) otherwise they will be converted to strings. when you modified the table it stopped being an array.

In this case, a possible solution would be to use strings as indexes in that table from the beginning.

I played around with this and I ended up doing: inventoryItemType[GetNewItemSlot(inventoryItemType, item2Slot)] = {[item1] = getItemAmt(inventoryItemType, item1)}
(I made 2 new functions for getting amount and the new slot)
I did this for the modified item function but when I update the client it doesn’t update. Am i doing something wrong?

I don’t see anything wrong with that line. But if your table meets the condition of an array and also has other elements, they are not replicated (only the array part will be replicated). You can read the part about parameter limitations in the documentation.

in theory it should work because I had no issue before I was changing the number. Im just not sure what is stopping it. Is there a way I can check to see if the type of value is correct?

well, you can always use type or typeof. But it seems to me that the problem lies in two places:

Tables with numeric indexes: in general, you should be very careful with tables with numeric indexes. An array always ends at the first nil element. for example, in this table: {[1] = 1, [7] = 2} the array part is {[1] = 1} because the second element is nil. There are also strange cases that you have to be very careful with.

RemoteEvent/RemoteFunction: these must serialize the data before being sent. This causes serious limitations on what can be sent (as it says in the documentation). For example, if you send the table {[1] = 1, [7] = 2} on the client you will only receive {[1] = 1} because the second element is no longer considered part of the array. On the other hand, if you send the table {[2] = 1, [7] = 2} on the client you will receive {[“2”] = 1, [“7”] = 2} because the table did not meet the array condition to begin with and the indexes are automatically converted to strings.

What do you mean by the second part is nil?

I used typeof to get the number value which printed out number for both of the functions that I used to get the value.