• Net library help
    10 replies, posted
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 [CODE] 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[/CODE] CLIENT [CODE] 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[/CODE]
[code] 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 [/code]
[QUOTE=Sir TE5T;51103640][code] 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 [/code][/QUOTE] 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.
[QUOTE=daxble;51103865]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.[/QUOTE] 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.
[QUOTE=Promptitude;51103870]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.[/QUOTE] No errors, and It's just in the autorun folder.
[QUOTE=daxble;51103889]No errors, and It's just in the autorun folder.[/QUOTE] Instead of just running the console command, try adding a print as well. So you can see if it's actually ran: [CODE]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[/CODE]
[QUOTE=Promptitude;51103908]Instead of just running the console command, try adding a print as well. So you can see if it's actually ran: [CODE]if SERVER then print( "Code loaded on the server" ) util.AddNetworkString("NetIdentifier") hook.Add( "PlayerSpawn", "HookIdentifier", function( ply ) print( "Hook ran" ) net.Start( "NetIdentifier" ) net.WriteString( ply:Team() == FACTION_MILITARY and "4" or "1" ) net.Send( ply ) end else print( "Code loaded on the client" ) net.Receive( "NetIdentifier", function() RunConsoleCommand("cw_kk_ins2_rig ", net.ReadString()) print("I ran!") end ) end[/CODE][/QUOTE] Should probably add more to see where the code breaks (Edited the quote)
Now it does this [CODE] 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! [/CODE]
[QUOTE=daxble;51103935]Now it does this [CODE] 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! [/CODE][/QUOTE] Ah, you don't actually need to add in the spaces, the function does that for you: [CODE]RunConsoleCommand("cw_kk_ins2_rig", net.ReadString())[/CODE] instead of: [CODE]RunConsoleCommand("cw_kk_ins2_rig ", net.ReadString())[/CODE]
Thank you so much for the help it's working correctly. For anyone else who needs it: [CODE] 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 [/CODE]
Sorry, you need to Log In to post a reply to this thread.