Net library seems to send wrong numbers

The error (and output of the print):
[pre]

[ERROR] addons/shooter/gamemodes/shooter/gamemode/cl_init.lua:154: bad argument #1 to ‘pairs’ (table expected, got nil)

  1. pairs - [C]:-1
  2. func - addons/shooter/gamemodes/shooter/gamemode/cl_init.lua:154
    3. unknown - lua/includes/modules/net.lua:32

[/lua]
0

Server code:
[lua]function GM:OnPlayerChangedTeam(ply, ot, nt)
net.Start(“clasui”)
print(nt)
net.WriteFloat(nt)
net.Send(ply)
ply:SetPlayerColor(team.GetColor(nt))
–if ply == LocalPlayer then

--end

end[/lua]

Client code:
[lua]
net.Receive(“clasui”, function(length, client)
tframe:MakePopup()
print(net.ReadFloat())
for n, c in pairs(team.GetClass(net.ReadFloat(nt))) do
classList:AddLine©
end
end)
[/lua]

net.ReadFloat won’t have 100% accuracy since lua_Number (the default lua number type in gmod) is a double

try net.Read/WriteDouble

or if it’s a whole number try Read/WriteUInt

EDIT: i didn’t realize you read two floats, read robotboy’s suggestion

You write one float but you read two.

Do this


 net.Receive("clasui", function(length, client)
	tframe:MakePopup()
local a = net.ReadFloat()
	print(a)
	for n, c in pairs(team.GetClass(a)) do
		classList:AddLine(c)
	end
 end)

After trying both solutions… nothing. New code:

[lua]
function GM:OnPlayerChangedTeam(ply, ot, nt)
net.Start(“clasui”)
print("nt - "…nt)
net.WriteDouble(nt)
net.Send(ply)
ply:SetPlayerColor(team.GetColor(nt))
–if ply == LocalPlayer then

--end

end
[/lua]

and

[lua]
net.Receive(“clasui”, function(length, client)
tframe:MakePopup()
x = net.ReadDouble(nt)
print(x)
for n, c in pairs(team.GetClass(x)) do
classList:AddLine©
end
end)
[/lua]

Is there an error? What’s printed? Does the net.Receive get called?

  1. Yes: [pre]
    [ERROR] addons/shooter/gamemodes/shooter/gamemode/cl_init.lua:155: bad argument #1 to ‘pairs’ (table expected, got nil)
  2. pairs - [C]:-1
  3. func - addons/shooter/gamemodes/shooter/gamemode/cl_init.lua:155
    3. unknown - lua/includes/modules/net.lua:32
    [/pre]
  4. Nothing. I don’t know why.
  5. Yes, or there would be no error.

Why not write the teams as integers? Unless I’m missing something here.
Also it doesn’t seem like you’re reading the doubles correctly, where is nt defined in the clientside code?

Tried that same error

Show the code you used when you tried to write the team as an integer.
I have a feeling you aren’t correctly using the bits argument.

[lua]
function GM:OnPlayerChangedTeam(ply, ot, nt)
net.Start(“clasui”)
print("nt - "…nt)
net.WriteInt(nt, 2)
net.Send(ply)
ply:SetPlayerColor(team.GetColor(nt))
–if ply == LocalPlayer then

--end

end[/lua]

There are two teams, 0 and 1

Can you post the clientside code as well?
Have you tried using a larger amount of bits as well, just in case somehow the team indexes got fucked up somehow and are higher than you expected?

[lua]
function GM:OnPlayerChangedTeam(ply, ot, nt)
print("nt - "…nt)

net.Start("clasui")
	net.WriteUInt(nt, 8) -- 8 bits, unsigned
net.Send(ply)

ply:SetPlayerColor(team.GetColor(nt)) -- Doesn't this function take a VECTOR?

end[/lua]
[lua]
net.Receive(“clasui”, function(length, client)

x = net.ReadUInt(8)
print("GOT EM: " .. x)

for n, c in pairs( team.GetClass(x) ) do
	classList:AddLine(c)
end

tframe:MakePopup()

end)[/lua]

Use this.

Look for GOT EM: number in your CLIENT console.

Make sure you set up classes SHARED, not only serverside.

[editline]3rd April 2015[/editline]

net.Receive MUST BE OUTSIDE OF ANY OTHER FUNCTION