net.Send/net.Read

Hi! (I’m betting code_gs will respond to this one too :P)

I’m fresh into learning net.Send/net.Read. My goal is to send a number from server to client.

I tried using net.WriteInt(), but the number I’m sending over is a variable. When declaring the bit for net.ReadInt(), I didn’t know that to put down.
Trying to do a workaround, I wanted to try sending it via net.WriteString(). This works, but when I try to change it back into a number, it doesn’t return anything.

Question 1: If I use net.ReadInt(), what do I put for the bit amount knowing that the variable can be in the range of 100-30,000?

Question 2: Is it possible to translate a string into a number using net.ReadString()?

Thanks for any answers!

1)If you know that your var is between 100 and 30000 just do 15, since 2^15 = 32k
The in ReadInt just read 15 again

  1. No…DONT do that please, numbers are numbers and strings strings, if you are trying to translate text input like chat into number, then do tonumber and check if it’s nil, but please don’t do bad practices

Sorry I’m not code_gs but,

  1. 2 ^ 16 = 65536, so 16 bits would work fine with a bit of leg room

  2. If you really need to, you can use https://wiki.garrysmod.com/page/Global/tonumber to convert a string to a number.

Yeah roger that. I was just seeing if I was going to get away with it.
So the bit value is the maximum number read?

[editline]19th September 2017[/editline]

Doesn’t seem to be working.

The net.Receive() is registering, but not outputting the variable.

Send:



net.Start("opr_moneyspent")
	net.WriteInt(charge, 15)
net.Send(ply)

Receive:


net.Receive("opr_moneyspent", function()
	chat.AddText(Color(255,255,0),"Charge: $",Color(0,255,0), net.ReadInt(15))
end)

Returning: “Charge: $”

[del]Have you

util.AddNetworkString’d the network string?[/del]

Are you sure the value you’re sending isn’t nil?

I used the same var with WriteString and it returned correctly, just to fool proof, ill add both and compare.

[editline]19th September 2017[/editline]

Yep. WriteString working fine.

Send:



net.Start("opr_moneyspent")
	net.WriteInt(charge, 15)
	net.WriteString(charge)
net.Send(ply) 

Receive:



net.Receive("opr_moneyspent", function()
	chat.AddText(Color(255,255,0),"Int - Charge: $",Color(0,255,0), net.ReadInt(15))
	chat.AddText(Color(255,255,0),"String - Charge: $",Color(0,255,0), net.ReadString())
end)

Returning:
Int - Charge: $
String - Charge: $3000

Oh I forgot,

chat.AddText only accepts string and color type arguments afaik, use tostring on it.

And its as simple as that! Working perfectly Thank you very much guys. Good to know I can always turn to you for help :slight_smile: