Table, for, pairs and nil value error.

Hello, community.

In SH_ file i have this table:



ArrestedTable = {}


They should keep info about arrested players.

Sometimes it’s need to be updated for some players, so I am trying to send table to player:



function PAHookFunc(arrested, ntime, arrestor)

	local TableArrested = {
		Player = arrested,
		Time = ntime,
		Who = arrestor
	}

	table.insert( ArrestedTable, TableArrested ) --[[ Updating serverside table --]]
	
	net.Start("SendTable")
		net.WriteTable( ArrestedTable ) --[[ Sending updated table to some client --]]
	net.Send(SOMECLIENT) --[[ It's not error, I put "SOMECLIENT" here for example --]]

end
hook.Add("playerArrested", "PAHook", PAHookFunc)


Clientside receive code:



function ReceiveArTable()
	local ply = LocalPlayer()
	if not ply:IsValid() or not ply:Alive() then
		return
	end
	table.Empty(ArrestedTable) --[[ Deleting outdated info in table --]]
	table.insert(ArrestedTable, net.ReadTable()) --[[ Update clientside table --]]
end
net.Receive("SendTable", ReceiveArTable)


(CLIENTSIDE)How i am trying to use this table, example code:



	for k, v in pairs( ArrestedTable ) do
		if v.Player:isArrested() then 
			MsgC(v.Player:Nick())
		end
	end --[[ Print in console for all arrested players. --]]


But I am getting this error:



attempt to index field 'Player' (a nil value)


I’ve checked the table and it’s seems ok.
PrintTable result:



1:
		1:
				Time	=	120
				Player	=	Player [4][Bot43]
				Who	=	Player [1][FloopyTruck!]
		2:
				Time	=	120
				Player	=	Player [2][Bot41]
				Who	=	Player [1][FloopyTruck!]
		3:
				Time	=	120
				Player	=	Player [3][Bot42]
				Who	=	Player [1][FloopyTruck!]
		4:
				Time	=	120
				Player	=	Player [6][Bot45]
				Who	=	Player [1][FloopyTruck!]


What I am doing wrong?

Will be glad for any help, thanks.

When the client receives the new table, the net.ReadTable() gives the WHOLE ArrestedTable, but you’re inserting it into their existing ArrestedTable. So the error is when you do ArrestedTable[1].Player, but ArrestedTable[1] is the actual ArrestedTable.

Fix it by:

Remove the lines on the client of table.Empty and table.Insert

replace them with

ArrestedTable = net.ReadTable()

Thank you for help, as I assumed it’s was fault with my logic in how it works.

/solved

ClientSide Receive Code:



function ReceiveArTable()
	local ply = LocalPlayer()
	if not ply:IsValid() or not ply:Alive() then
		return
	end
	ArrestedTable = net.ReadTable() --[[ Overwriting clientside table --]]
end
net.Receive("SendTable", ReceiveArTable)