• Unable to send a normal number to the server
    3 replies, posted
I'm making a couple very simple commands to practice networking. Right now I'm trying to just simply send over the argument of set_health to the server, so that the server sets the health of the player(which ran the command) to said argument. The problem I'm having is that I am unable to send a "normal" number to the server, or even a string( I tried making it a string and then changing it to a number on the server). I might just be using the wrong bit setting, but I honestly don't understand them enough to really get the right one if that's the case. Could anyone please explain how does one send a number to the server without getting a weird result? this is basically what I have going: [code] if (SERVER) then util.AddNetworkString( "SetHealth" ) net.Receive( "SetHealth" , function( len , ply ) if IsValid( ply ) and ply:IsPlayer() then local PlayerSetHP = net.ReadInt( 32 ) local PlayerSetHP = tonumber( PlayerSetHP ) print( PlayerSetHP ) print( type( PlayerSetHP ) ) ply:SetHealth( PlayerSetHP ) end end ) end if (CLIENT) then concommand.Add( "Set_Health" , function( ply , cmd , args ) if args[1] then PlayerSetHP = tonumber( args[1] ) end print( PlayerSetHP ) print( type( PlayerSetHP ) ) net.Start( "SetHealth" ) net.WriteEntity( ply ) net.WriteInt( PlayerSetHP , 32 ) net.SendToServer() end ) end [/code] whenever I try to set the health, I get something like this: [code] -- The print results( for debugging ) - server 65537 -- the number that the server receives and sets the health to. number -- just the type of variable it is. [/code]
Its because you have to read them in order on the server. You sent the player entity so you would have to use net.Readentity() before you use net.ReadInt(). You also don't have to send the player entity to the server anyway, so just remove net.WriteEntit(ply) client side and it should all work.
[QUOTE=Nick78111;52186877]Its because you have to read them in order on the server. You sent the player entity so you would have to use net.Readentity() before you use net.ReadInt(). You also don't have to send the player entity to the server anyway, so just remove net.WriteEntit(ply) client side and it should all work.[/QUOTE] Thank you. I didn't really understand what they ment by that in the wiki.
[code] net.WriteEntity( ply ) [/code] It seems like you're sending over whoever ran the command, which in that case you don't need to do net.WriteEntity. It's bad practice and potentially exploitable in this situation. net.Receive has an argument for the sender. Also you don't need to tonumber net.ReadInt() because it's already an integer (hence the name).
Sorry, you need to Log In to post a reply to this thread.