Array looping bullshit

Okay, the code I am using works, except for one problem. The arrays are send back-the-front.

So serverside, the array is stored for example,



[0] = "Watermelon"
[1] = "Watermelon2"


And clientside, it is stored as



[1] = "Watermelon"
[0] = "Watermelon2"


The code I am using is as follows.

Serverside:
[lua]
local derp = 0
for k, v in pairs(ItemNameArray) do
timer.Create(ply:SteamID()…“asd”…derp, 0.1, 1, function()
umsg.Start(“SendItemInfo”)
umsg.Long(1)
umsg.String(v)
umsg.End()
end)
derp = derp + 1
end
[/lua]

Clientside:
[lua]
function SetItemArray( a )
if a:ReadLong() == 1 then
ItemNameArray[noitem] = a:ReadString()
//LocalPlayer():ChatPrint(ItemNameArray[noitem])
noitem = noitem + 1
end
//Msg("["…noitem…"] = “…ItemNameArray[noitem - 1]…”
")
end

usermessage.Hook(“SendItemInfo”, SetItemArray)
[/lua]

I tried to change the serverside code to this:
[lua]
derp = 0
local derp = 0
while derp < table.Count(ItemNameArray) + 1 do
timer.Create(ply:SteamID()…“asd”…derp, 0.1, 1, function()
umsg.Start(“SendItemInfo”)
umsg.Long(1)
umsg.String(ItemNameArray[derp])
umsg.End()
end)
derp = derp + 1
end
[/lua]

but it gave me an error for the line with umsg.String(ItemNameArray[derp]) line, stating that I had used a nil value instead of a string.

The table ItemNameArray starts at 0

Start your keys at 1 then move to 2 and so on if using numerical keys. If not it will return in the order sent. I think it’s returning like that because it looks for 1 first.

So, do you want me to start both arrays at 1?

Yes.

I did that, it worked, then I added another item to the array.
Clientside Array:



1	=	Watermelon
2	=	Watermelon3
3	=	Watermelon2


Serverside:



1	=	Watermelon2
2	=	Watermelon3
0	=	Watermelon


[editline]9th July 2012[/editline]

So yes, just to clarify, there is still a problem

Untested should work:

clientside:
[lua]
function SetItemArray(data)
local NumItems = data:ReadShort()
for i = 1, NumItems do
local vals = data:ReadString()
print(i, vals)
end
end
usermessage.Hook(“SendItemInfo”, SetItemArray)
[/lua]

serverside:
[lua]
local Watermelons = {}
Watermelons[1] = “Watermelon”
Watermelons[2] = “Watermelon2”

function WatermelonUM(ply, cmd, args)
umsg.Start(“SendItemInfo”, ply)
umsg.Short(#Watermelons)
for i = 1, #Watermelons do
umsg.String(Watermelons*)
end
umsg.End()
end
concommand.Add(“startUM”, WatermelonUM)
[/lua]

Your problem is that you used pairs, Not Ipairs while sending the table.
pairs retrives the values in a random order, Where as ipairs retrives them alphanumericly.