net.Receive always receiving the same value

Hey, I’m working on a simple menu that the player will use to choose their team, and I’m using the net library to send information between the client and server. However, whenever the server receives the client’s info, it always receives 0. Here is my code:

init.lua


-- ///   FILES   ///
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_menus.lua")

--include("init.lua")
include("shared.lua")
include("player.lua")
-- /// END FILES ///

-- ///   NETWORKING   ///
util.AddNetworkString("cl_OpenTeamMenu")
util.AddNetworkString("sv_OpenTeamMenu")
-- /// END NETWORKING ///

function GM:PlayerConnect( name, ip )
	print("Player: " .. name .. ", has joined the game.")
end

function GM:PlayerInitialSpawn( ply )
	print("Player: " .. ply:Nick() .. " has spawned.")
	GAMEMODE:SetPlayerSpeed( ply, 225, 350 )
	
	net.Start("cl_OpenTeamMenu")
	net.Send(ply)
	net.Receive("sv_OpenTeamMenu", function( len )
		TeamResult = net.ReadFloat()
		print( TeamResult )
		ply:SetGamemodeTeam( TeamResult )
	end )
end
hook.Add( "Player Spawn", "playerSetSpeedtest", playerSetSpeed )

function GM:PlayerSpawn( ply )
	n = ply:Team()
	ply:SetGamemodeTeam( n )
	ply:AllowFlashlight(true)
end

function GM:PlayerAuthed( ply, steamID, uniqueID )
	print("Player: " .. ply:Nick() .. " has been authorized.")
end

function GM:PlayerShouldTakeDamage( ply, victim )
	if ply:IsPlayer() or ply:IsAdmin() or ply:IsSuperAdmin() then
		if ply:Team() == victim:Team() then
			return false
		end
	end
	return true
end

cl_menus.lua


net.Receive("cl_OpenTeamMenu", function() OpenTeamMenu() end)

function OpenTeamMenu()
	local ply = LocalPlayer()
	
	-- Background
	local BackGround = vgui.Create("DFrame")
	BackGround:SetSize(600, 150)
	BackGround:SetPos( (ScrW()/2)-(BackGround:GetWide()/2),(ScrH()/3)-(BackGround:GetTall()/2) )	-- Set the position to center of the screen
	BackGround:SetTitle("Team Selection")
	BackGround:SetVisible(true)
	BackGround:SetDraggable(false)
	BackGround:ShowCloseButton(true)		-- Don't forget to set this to false on initial run!
	BackGround:MakePopup()
	
	-- List
	List = vgui.Create("DPanelList", BackGround)
	List:SetSize(560, 180)
	List:SetPos(20, 40)
	List:SetSpacing(10)						-- Set spacing between items
	List:EnableHorizontal(false)			-- Only allow vertical items
	List:EnableVerticalScrollbar(true)		-- Allow scrollbar if y axis is exceeded
	
	-- Team 1 Button
	local Team1 = vgui.Create("DButton")
	Team1:SetText("SWAT")
	Team1:SetSize(560, 40)
	Team1.Paint = function()
		surface.SetDrawColor(70, 70, 255)						-- Inside of box
		surface.DrawRect(0, 0, Team1:GetWide(), Team1:GetTall())
		surface.SetDrawColor(255, 255, 255)							-- Outline of box
		surface.DrawOutlinedRect(0, 0, Team1:GetWide(), Team1:GetTall())
	end
	Team1.DoClick = function()
		TeamResult = 0		-- Team 1 is entry 0 in the teams table; send this to server
		TeamMenuResult()
	end
	List:AddItem(Team1) -- Add to list
	
	-- Team 2 Button
	local Team2 = vgui.Create("DButton")
	Team2:SetText("Rebels")
	Team2:SetSize(560, 40)
	Team2.Paint = function()
		surface.SetDrawColor(255, 70, 70)						-- Inside of box
		surface.DrawRect(0, 0, Team1:GetWide(), Team1:GetTall())
		surface.SetDrawColor(255, 255, 255)							-- Outline of box
		surface.DrawOutlinedRect(0, 0, Team1:GetWide(), Team1:GetTall())
	end
	Team2.DoClick = function()
		TeamResult = 1		-- Team 2 is entry 1 in the teams table; send this to server
		TeamMenuResult()
	end
	List:AddItem(Team2) -- Add to list
	
end
concommand.Add("blk_teammenu", OpenTeamMenu)

function TeamMenuResult()
	net.Start("sv_OpenTeamMenu")
		net.WriteFloat(TeamResult)
	net.SendToServer()
end

player.lua


local ply = FindMetaTable("Player")

-- /////   TEAM TABLE   /////
local teams = {}
--          Team 1 (SWAT) = Blue, Team 2 (Rebels) = Red
teams[0] = {name = "SWAT", color = Vector( .2, .2, 1.0 ), weapons = {"m9k_knife", "m9k_dbarrel"} }
teams[1] = {name = "Rebels", color = Vector( 1.0, .2, .2 ), weapons = {"m9k_knife", "m9k_dbarrel"} }
-- ///// END TEAM TABLE /////

function ply:SetGamemodeTeam( n )
	if not teams[n] then return end		-- If the selected team does not exist, return
	
	self:SetTeam( n )						-- Set team
	self:SetPlayerColor( teams[n].color )	-- Set player model color according to the team
	self:GiveGamemodeWeapons()				-- Distribute weapons
	self:ApplyCharacterModel()				-- Randomize and set character model
	
	return true
end

function ply:GiveGamemodeWeapons()
	local n = self:Team()
	self:StripWeapons()
	
	for k, wep in pairs(teams[n].weapons) do
		self:Give(wep)
	end
end

function ply:ApplyCharacterModel()
	local n = self:Team()
	
	-- //   PLAYER MODEL RANDOMIZATION   //
	if n == 0 then
		self:SetModel("models/player/Police.mdl")
	elseif t == 1 then
		local modelDice = math.random(1, 9)
		if modelDice == 1 then
			self:SetModel("models/player/Group03/Male_01.mdl")
		elseif modelDice == 2 then
			self:SetModel("models/player/Group03/Male_02.mdl")
		elseif modelDice == 3 then
			self:SetModel("models/player/Group03/Male_03.mdl")
		elseif modelDice == 4 then
			self:SetModel("models/player/Group03/Male_04.mdl")
		elseif modelDice == 5 then
			self:SetModel("models/player/Group03/Male_05.mdl")
		elseif modelDice == 6 then
			self:SetModel("models/player/Group03/Male_06.mdl")
		elseif modelDice == 7 then
			self:SetModel("models/player/Group03/Male_07.mdl")
		elseif modelDice == 8 then
			self:SetModel("models/player/Group03/Male_08.mdl")
		else
			self:SetModel("models/player/Group03/Male_09.mdl")
		end
	end
	
	-- // END PLAYER MODEL RANDOMIZATION //
end

You must not put net receive in a hook/function. Put it outside of any functions.

Try printing the value in TeamMenuResult func.

Thanks for your help, I’m new to the lua language.

I’ve moved net.Receive outside of the GM:PlayerInitialSpawn function, but when it prints the value, it’s still zero. I’m also getting a new error:


gamemodes/blackout/gamemode/player.lua:13: Tried to use a NULL entity!
  1. SetTeam - [C]:-1
    2. SetGamemodeTeam - gamemodes/blackout/gamemode/player.lua:13
      3. func - gamemodes/blackout/gamemode/init.lua:33
        4. unknown - lua/includes/modules/net.lua:31

This is what my new net.Receive looks like:


net.Receive("sv_OpenTeamMenu", function()
	local ply = FindMetaTable( "Player" )
	TeamResult = net.ReadFloat()
	print( TeamResult )
	ply:SetGamemodeTeam( TeamResult )
end )

Also, @Netheous:

The TeamMenuResult func is in a client side file, and I don’t think I can print to the console from there (or if I can I’m too new to know how).

You can but it prints to the clients console

You aren’t getting player correctly there.

[lua]net.Receive(“sv_OpenTeamMenu”, function(len,ply)
TeamResult = net.ReadFloat()
print( TeamResult )
ply:SetGamemodeTeam( TeamResult )
end )[/lua]

You should also check the player is still valid in case they’ve become invalid in the time it took the message to be received.

Thanks so much, I apologize I’m so new to this. I’ve made the changes posted, but the server is still receiving a value of 0 no matter which button is pressed.

I’ve made some modifications to the code you posted which I believe should work.

I’ve changed cl_menus.lua slightly. Is there a reason you are storing the number in TeamResult? I’ve changed button1 and 2 .DoClick to call the TeamMenuResult function by passing the number to it instead of storing it in TeamResult. Then I’ve modified the TeamMenuResult function to print what number it is sending, so you can see whether the client is sending the correct number to the server.
I’ve also changed it to send an integer instead of a float.

cl_menus.lua
[lua]net.Receive(“cl_OpenTeamMenu”, function() OpenTeamMenu() end)

local function TeamMenuResult(chosenTeam)
print(“Sending players team choice to the server”)
net.Start(“sv_OpenTeamMenu”)
print(“We are sending”,chosenTeam)
net.WriteInt(chosenTeam,16) --writes a 16bit integer (you could go even smaller, 8bits, but it doesn’t matter)
net.SendToServer()
end

function OpenTeamMenu()
local ply = LocalPlayer()

-- Background
local BackGround = vgui.Create("DFrame")
BackGround:SetSize(600, 150)
BackGround:SetPos( (ScrW()/2)-(BackGround:GetWide()/2),(ScrH()/3)-(BackGround:GetTall()/2) )	-- Set the position to center of the screen
BackGround:SetTitle("Team Selection")
BackGround:SetVisible(true)
BackGround:SetDraggable(false)
BackGround:ShowCloseButton(true)		-- Don't forget to set this to false on initial run!
BackGround:MakePopup()

-- List
local List = vgui.Create("DPanelList", BackGround)
List:SetSize(560, 180)
List:SetPos(20, 40)
List:SetSpacing(10)						-- Set spacing between items
List:EnableHorizontal(false)			-- Only allow vertical items
List:EnableVerticalScrollbar(true)		-- Allow scrollbar if y axis is exceeded

-- Team 1 Button
local Team1 = vgui.Create("DButton")
Team1:SetText("SWAT")
Team1:SetSize(560, 40)
Team1.Paint = function()
	surface.SetDrawColor(70, 70, 255)						-- Inside of box
	surface.DrawRect(0, 0, Team1:GetWide(), Team1:GetTall())
	surface.SetDrawColor(255, 255, 255)							-- Outline of box
	surface.DrawOutlinedRect(0, 0, Team1:GetWide(), Team1:GetTall())
end
Team1.DoClick = function()
	TeamMenuResult(0) -- Team 1 is entry 0 in the teams table; send this to server
end
List:AddItem(Team1) -- Add to list

-- Team 2 Button
local Team2 = vgui.Create("DButton")
Team2:SetText("Rebels")
Team2:SetSize(560, 40)
Team2.Paint = function()
	surface.SetDrawColor(255, 70, 70)						-- Inside of box
	surface.DrawRect(0, 0, Team1:GetWide(), Team1:GetTall())
	surface.SetDrawColor(255, 255, 255)							-- Outline of box
	surface.DrawOutlinedRect(0, 0, Team1:GetWide(), Team1:GetTall())
end
Team2.DoClick = function()
	TeamMenuResult()
end
List:AddItem(Team2) -- Add to list

end
concommand.Add(“blk_teammenu”, OpenTeamMenu)
[/lua]

I’ve modified init.lua to with the correct net.Receive, and changed it to read an integer instead of float.
[lua]-- /// FILES ///
AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)
AddCSLuaFile(“cl_menus.lua”)

–include(“init.lua”)
include(“shared.lua”)
include(“player.lua”)
– /// END FILES ///

– /// NETWORKING ///
util.AddNetworkString(“cl_OpenTeamMenu”)
util.AddNetworkString(“sv_OpenTeamMenu”)
– /// END NETWORKING ///

function GM:PlayerConnect( name, ip )
print("Player: " … name … “, has joined the game.”)
end

function GM:PlayerInitialSpawn( ply )
print(“Player: " … ply:Nick() … " has spawned.”)
GAMEMODE:SetPlayerSpeed( ply, 225, 350 )

net.Start("cl_OpenTeamMenu")
net.Send(ply)

end
hook.Add( “Player Spawn”, “playerSetSpeedtest”, playerSetSpeed )

function GM:PlayerSpawn( ply )
n = ply:Team()
ply:SetGamemodeTeam( n )
ply:AllowFlashlight(true)
end

function GM:PlayerAuthed( ply, steamID, uniqueID )
print(“Player: " … ply:Nick() … " has been authorized.”)
end

function GM:PlayerShouldTakeDamage( ply, victim )
if ply:IsPlayer() or ply:IsAdmin() or ply:IsSuperAdmin() then
if ply:Team() == victim:Team() then
return false
end
end
return true
end

net.Receive(“sv_OpenTeamMenu”, function( len, ply )
local TeamResult = net.ReadInt(16)
print( TeamResult )
ply:SetGamemodeTeam( TeamResult )
end )[/lua]

[editline]14th February 2014[/editline]

This won’t affect the above code at all but will help you in the future, you can make your paint functions more generic.

Instead of
[lua]Team1.Paint = function()
surface.SetDrawColor(70, 70, 255) – Inside of box
surface.DrawRect(0, 0, Team1:GetWide(), Team1:GetTall())
surface.SetDrawColor(255, 255, 255) – Outline of box
surface.DrawOutlinedRect(0, 0, Team1:GetWide(), Team1:GetTall())
end[/lua]

You can do
[lua]Team1.Paint = function(self,w,h)
surface.SetDrawColor(70, 70, 255) – Inside of box
surface.DrawRect(0, 0, w, h)
surface.SetDrawColor(255, 255, 255) – Outline of box
surface.DrawOutlinedRect(0, 0, w, h)
end[/lua]

The paint function automatically gets called with the first argument being the panel being painted, and the second and third argument are it’s current size, you don’t need to lookup the size again.

Hi wh1t3rabbit, thanks for your reply.
I tried out the code that you sent, but the problem persists, the server still receives 0 regardless of which button was pressed (Also you forgot to use the new parameter when calling TeamMenuResult in Team2.DoClick, but the problem persisted after fixing that, too). Thanks for the help, though.

So on the client it prints “We are sending”,1 , but then server receives 0?

It didn’t print anything in either the server or player consoles at that part, it just printed the 0 in the server console as usual.