Net library help

I’ve been trying to get the know the net library so I’m trying to make a simple script that when the play spawns it sets a convar on client’s side. I know I’m doing something wrong (otherwise I wouldn’t be here). I would appreciate if anyone could help me.

SERVER



if SERVER then

	util.AddNetworkString("talibanmessage")
	util.AddNetworkString("militarymessage")
	
	funtion GM:PlayerSpawn( ply )
		if ply:Team() == FACTION_MILITARY then
			net.Start("militarymessage")
			net.WriteEntity( ply )
		elseif ply:Team() == FACTION_TALIBAN then
			net.Start("talibanmessage")
			net.WriteEntity( ply )
                end
	end
end

CLIENT



if CLIENT then
	
	net.Receive("militarymessage", function()
		if LocalPlayer() == net.ReadEntity then
			RunConsoleCommand("cw_kk_ins2_rig ", "4")
		end
	end)
	
	net.Receive("talibanmessage", function()
		if LocalPlayer() == net.ReadEntity then
			RunConsoleCommand("cw_kk_ins2_rig ", "1")
		end
	end)
	
end



hook.Add( "PlayerSpawn", "MilitaryMessaging", function( ply )
	if ( ply:Team() == FACTION_MILITARY ) then 
		ply:ConCommand( "cw_kk_ins2_rig 4" )
	elseif ( ply:Team() == FACTION_TALIBAN ) then 
		ply:ConCommand( "cw_kk_ins2_rig 1" )
	end
end


While that is the best way to do what he’s attempting to do, I believe the point of the script is just to practice the net library, so it would be done like this:

[lua]if SERVER then

util.AddNetworkString("NetIdentifier")

hook.Add( "PlayerSpawn", "HookIdentifier", function( ply )
	net.Start( "NetIdentifier" )
	net.WriteString( ply:Team() == FACTION_MILITARY and "4" or "1" )
	net.Send( ply )
end

else
net.Receive( “NetIdentifier”, function()
RunConsoleCommand("cw_kk_ins2_rig ", net.ReadString())
end )
end[/lua]

I know I’m just being stupid, but I can’t get either of these solutions to work, sorry for the trouble it’s my first time using the Net Library.

Is there any kind of error, and where are you putting the file :P? It needs to be somewhere where it’s ran in both client and server.

No errors, and It’s just in the autorun folder.

Instead of just running the console command, try adding a print as well. So you can see if it’s actually ran:


if SERVER then
 
	util.AddNetworkString("NetIdentifier")
	
	hook.Add( "PlayerSpawn", "HookIdentifier", function( ply )
		net.Start( "NetIdentifier" )
		net.WriteString( ply:Team() == FACTION_MILITARY and "4" or "1" )
		net.Send( ply )
	end
else
	net.Receive( "NetIdentifier", function()
		RunConsoleCommand("cw_kk_ins2_rig ", net.ReadString())
                print("I ran!")
	end )
end

Should probably add more to see where the code breaks (Edited the quote)

Now it does this



RunConsoleCommand: Command has invalid characters! (cw_kk_ins2_rig  (' '))
	The first parameter of this function should contain only the command, the second parameter should contain arguments.
I ran!


Ah, you don’t actually need to add in the spaces, the function does that for you:


RunConsoleCommand("cw_kk_ins2_rig", net.ReadString())

instead of:


RunConsoleCommand("cw_kk_ins2_rig ", net.ReadString())

Thank you so much for the help it’s working correctly.

For anyone else who needs it:



if SERVER then
 
	util.AddNetworkString("NetIdentifier")
	
	hook.Add( "PlayerSpawn", "HookIdentifier", function( ply )
		net.Start( "NetIdentifier" )
		net.WriteString( ply:Team() == FACTION_MILITARY and "4" or "1" )
		net.Send( ply )
	end )
else
	net.Receive( "NetIdentifier", function()
		RunConsoleCommand("cw_kk_ins2_rig", net.ReadString())
	end )
end