Gmod Network error.

Hi, I’m just new in gmod lua programming and I still didn’t get it how to create a network and so on. I tried it but I got an error message.

So what I want to do is to create a network between the client and the server. Something like this.

CLIENT
[Lua]
net.Start( “DOAMESS” )
net.WriteBool( Deadoralive )
net.WriteBool( Dead )
net.WriteBool( Alived )
net.WriteFloat( Pricealive )
net.WriteFloat( Pricedead )
net.SendToServer()
[/Lua]

SERVER
[Lua]
net.Receive( “DOAMESS”, function(len, ply)
MsgN(“DOAMESS: Contains “…len…” bits”)
MsgN("Player: "…ply)

MsgN("Dead or alive?")
MsgN(net.ReadBool())  

MsgN("Dead?")
MsgN(net.ReadBool())

MsgN("Alive?")
MsgN(net.ReadBool())

MsgN("Price alive?")
MsgN(net.ReadFloat())

MsgN("Price dead?")
MsgN(net.ReadFloat())

end)
[/Lua]

Btw is want it to send the data that you check / write in the client so the server can do what it wants.

Did you do

util.AddNetworkString serverside with “DOAMESS” as the string? You need to do that for the message to work

[editline]28th March 2016[/editline]

If you did do that but it still isn’t working, could you post the error?

[editline]28th March 2016[/editline]

Also, are you sure you defined all those variables in your clientside code?

:snip:

:snip:

What? :weeb:

[editline]28th March 2016[/editline]

Well now it prints me another error and I don’t know how to fix it ^^.

[Lua]

[ERROR] addons/dead or alive/lua/autorun/client/panel.lua:157: bad argument #1 to ‘WriteFloat’ (number expected, got nil)

  1. WriteFloat - [C]:-1
  2. unknown - addons/dead or alive/lua/autorun/client/panel.lua:157

[/Lua]

What are you writing exactly?

[Lua]
local DOATEXTE = vgui.Create( “DTextEntry”, DOAP )
DOATEXTE:SetPos( 410, 360 )
DOATEXTE:SetSize( 200, 20 )
DOATEXTE:SetText( “” )
DOATEXTE.OnEnter = function( self )
Pricedead = self:GetValue()
end

local DOATEXTE2 = vgui.Create( “DTextEntry”, DOAP )
DOATEXTE2:SetPos( 410, 420 )
DOATEXTE2:SetSize( 200, 20 )
DOATEXTE2:SetText( “” )
DOATEXTE2.OnEnter = function( self )
Pricealive = self:GetValue()
end
[/Lua]

I defined the floats.

Is the text meant to be a number? If it is, you have to do

Global.tonumber

Otherwise it returns a string

I’m writing an addon for DarkRP. The addon allows the mayor to type !wanted in chat and is allowed to make a player wanted. He can choose dead/ alive or dead or alive and he can say how much money the player gets when he kills him or brings him alive to pd.

:snip: damn automerge

If you want the full code that I wrote just tell me if you need it to help me.

I don’t think I need any more code at the moment - if you do something like



Pricealive = tonumber( self:GetValue() )


It should be okay (but you should check if the text is actually a valid number first, e.g. by checking if it’s not nil)

Well it says it’s a nil value. :v:

[editline]28th March 2016[/editline]

[Lua]
[ERROR] addons/dead or alive/lua/autorun/client/panel.lua:157: bad argument #1 to ‘WriteFloat’ (number expected, got nil)

  1. WriteFloat - [C]:-1
  2. unknown - addons/dead or alive/lua/autorun/client/panel.lua:157
    [/Lua]

I really have to learn lua again

[editline]28th March 2016[/editline]

-snip-

Wait, try printing the text once it’s been entered, maybe that’ll tell you something

[editline]28th March 2016[/editline]

Are you sure you’re only entering a number and nothing else into it?

Well I’m not that good in explaining and I tried to print it and it gave me nothing but well, you will see the code now and know whats the problem because im pretty sure I have made a big mistake somewhere.

CLIENT

[Lua]
hook.Add( “OnPlayerChat”, “HelloCommand”, function( ply, strText, bTeam, bDead )

strText = string.lower( strText )

if ( strText == "!wanted" ) then

DOAP = vgui.Create( “DFrame” )
DOAP:SetSize( 1000, 500 )
DOAP:SetText( “” )
DOAP:Center()
DOAP:SetTitle("")
DOAP:SetVisible( true )
DOAP:SetDraggable( false )
DOAP:ShowCloseButton( false )
DOAP:MakePopup()
DOAP.Paint = function(s, w, h)
draw.RoundedBox(5, 0, 0, w, h, Color(0,0,0))
draw.RoundedBox(5, 2, 2, w-4, h-4, Color(50, 50, 50))
end

local DOAT2 = vgui.Create( “DLabel” , DOAP )
DOAT2:SetPos( 465, 20 )
DOAT2:SetText( “Dead or Alive” )
DOAT2:SetSize(200, 20)

local DOAB = vgui.Create( “DButton” , DOAP )
DOAB:SetPos( 977, 3)
DOAB:SetText( “X” )
DOAB:SetSize( 20, 20 )
DOAB:SetTextColor( Color(255,255,255))
DOAB.Paint = function(s, w, h)
draw.RoundedBox(3,0,0,w,h,Color(255, 50, 50))
end
DOAB.DoClick = function()
DOAP:Close()
end

local DOAC = vgui.Create( “DComboBox” , DOAP )
DOAC:SetPos( 400, 150 )
DOAC:SetSize( 200, 20 )
DOAC:SetValue( “Player” )
for k, v in pairs( player.GetAll() ) do
DOAC:AddChoice ( tostring( v:Name() ) )
end

local DOAT = vgui.Create( “DLabel” , DOAP )
DOAT:SetPos( 460, 35 )
DOAT:SetText( “Wanted menu” )
DOAT:SetSize(200,200)

local DOAB2 = vgui.Create( “DButton” , DOAP )
DOAB2:SetPos( 390, 180)
DOAB2:SetText( “Wanted” )
DOAB2:SetSize( 100, 20 )
DOAB2:SetTextColor( Color(255,255,255))
DOAB2.Paint = function(s, w, h)
draw.RoundedBox(3,0,0,w,h,Color(255, 50, 50))
end
DOAB2.DoClick = function()
wanted = true
unwanted = false
end

local DOAB3 = vgui.Create( “DButton” , DOAP )
DOAB3:SetPos( 510, 180)
DOAB3:SetText( “Unwanted” )
DOAB3:SetTextColor(Color(255,255,255))
DOAB3:SetSize( 100, 20 )
DOAB3:SetTextColor( Color(255,255,255))
DOAB3.Paint = function(s, w, h)
draw.RoundedBox(3,0,0,w,h,Color(50, 180, 50))
end
DOAB3.DoClick = function()
unwanted = true
wanted = false
end

DOACH = vgui.Create(“DCheckBox”, DOAP)
DOACH:SetPos(495,250)
DOACH:Toggle()
function DOACH:OnChange( bVal )
if ( bVal ) then
DOACH2:SetValue()
DOACH3:SetValue()
Deadoralive = true
end
end

DOACH2 = vgui.Create(“DCheckBox”, DOAP)
DOACH2:SetPos(145,250)
function DOACH2:OnChange( bVal )
if ( bVal ) then
DOACH:SetValue()
DOACH3:SetValue()
Dead = true
end
end

DOACH3 = vgui.Create(“DCheckBox”, DOAP)
DOACH3:SetPos(845,250)
function DOACH3:OnChange( bVal )
if ( bVal ) then
DOACH2:SetValue()
DOACH:SetValue()
Alived = true
end
end

local DOAT3 = vgui.Create( “DLabel” , DOAP )
DOAT3:SetPos( 140, 230 )
DOAT3:SetText( “Dead” )
DOAT3:SetSize(200, 20)

local DOAT4 = vgui.Create( “DLabel” , DOAP )
DOAT4:SetPos(475, 230)
DOAT4:SetText( “Dead or Alive” )
DOAT4:SetSize(200, 20)

local DOAT5 = vgui.Create( “DLabel” , DOAP )
DOAT5:SetPos( 840, 230 )
DOAT5:SetText( “Alive” )
DOAT5:SetSize(200, 20)

local DOATEXTE = vgui.Create( “DTextEntry”, DOAP )
DOATEXTE:SetPos( 410, 360 )
DOATEXTE:SetSize( 200, 20 )
DOATEXTE:SetText( “” )
DOATEXTE.OnEnter = function( self )
Pricedead = tonumber( self:GetValue() )
end

local DOATEXTE2 = vgui.Create( “DTextEntry”, DOAP )
DOATEXTE2:SetPos( 410, 420 )
DOATEXTE2:SetSize( 200, 20 )
DOATEXTE2:SetText( “” )
DOATEXTE2.OnEnter = function( self )
Pricealive = tonumber( self:GetValue() )
end

local DOAT6 = vgui.Create( “DLabel” , DOAP )
DOAT6:SetPos( 485, 340 )
DOAT6:SetText( “Price dead” )
DOAT6:SetSize(200, 20)

local DOAT7 = vgui.Create( “DLabel” , DOAP )
DOAT7:SetPos( 485, 400 )
DOAT7:SetText( “Price alive” )
DOAT7:SetSize(200, 20)
return true
end

end )

net.Start( “DOAMESS” )
net.WriteBool( unwanted )
net.WriteBool( wanted )
net.WriteBool( Deadoralive )
net.WriteBool( Dead )
net.WriteBool( Alived )
net.WriteFloat( Pricealive )
net.WriteFloat( Pricedead )
net.SendToServer()
[/Lua]

SERVER

[Lua]
util.AddNetworkString( “DOAMESS” )
net.Receive( “DOAMESS”, function(len, ply)
MsgN(“DOAMESS: Contains “…len…” bits”)
MsgN("Player: "…ply)

MsgN("wanted?")
MsgN(net.ReadBool())  

MsgN("unwanted?")
MsgN(net.ReadBool())

MsgN("Dead or alive?")
MsgN(net.ReadBool())  

MsgN("Dead?")
MsgN(net.ReadBool())

MsgN("Alive?")
MsgN(net.ReadBool())

MsgN("Price alive?")
MsgN(net.ReadFloat())

MsgN("Price dead?")
MsgN(net.ReadFloat())

end)
[/Lua]

I think the problem is that you’re trying to start and send the net message OUTSIDE the hook

[editline]28th March 2016[/editline]

Try adding a ‘done’ button or something to your panel and make the net message send once the user has pressed it

[editline]28th March 2016[/editline]

Also, it wouldn’t hurt to make your DOAP panel local

I made now a done button and im trying to configurate it now. Btw if im setting DOAP to local it will print me couple of errors.

[editline]28th March 2016[/editline]

I made it now like this and it wont print me errors. But im not sure if this is how you ment it to be.

[Lua]
if send == true then
net.Start( “DOAMESS” )
net.WriteBool( unwanted )
net.WriteBool( wanted )
net.WriteBool( Deadoralive )
net.WriteBool( Dead )
net.WriteBool( Alived )
net.WriteFloat( Pricealive )
net.WriteFloat( Pricedead )
net.SendToServer()
end

[/Lua]

[editline]28th March 2016[/editline]

Button
[Lua]
local DOAB3 = vgui.Create( “DButton” , DOAP )
DOAB3:SetPos( 460, 460)
DOAB3:SetText( “Done” )
DOAB3:SetTextColor(Color(255,255,255))
DOAB3:SetSize( 100, 20 )
DOAB3:SetTextColor( Color(255,255,255))
DOAB3.Paint = function(s, w, h)
draw.RoundedBox(3,0,0,w,h,Color(50, 50, 255))
end
DOAB3.DoClick = function()
send = true
end
[/Lua]



DOAB3.DoClick = function()
	net.Start( "DOAMESS" )
		net.WriteBool( unwanted )
		net.WriteBool( wanted )
    		net.WriteBool( Deadoralive )
    		net.WriteBool( Dead )
    		net.WriteBool( Alived )
    		net.WriteFloat( Pricealive )
    		net.WriteFloat( Pricedead )
	net.SendToServer()
end


Thanks.

[editline]28th March 2016[/editline]

Now I edited it and I get this error message.

[Lua]
[ERROR] addons/dead or alive/lua/autorun/client/panel.lua:88: bad argument #1 to ‘WriteString’ (string expected, got nil)

  1. WriteString - [C]:-1
  2. DoClick - addons/dead or alive/lua/autorun/client/panel.lua:88
    3. unknown - lua/vgui/dlabel.lua:218
    [/Lua]

This is my code!

CLIENT

[Lua]
hook.Add( “OnPlayerChat”, “HelloCommand”, function( ply, strText, bTeam, bDead )

strText = string.lower( strText )

if ( strText == "!wanted" ) then

DOAP = vgui.Create( “DFrame” )
DOAP:SetSize( 1000, 500 )
DOAP:SetText( “” )
DOAP:Center()
DOAP:SetTitle("")
DOAP:SetVisible( true )
DOAP:SetDraggable( false )
DOAP:ShowCloseButton( false )
DOAP:MakePopup()
DOAP.Paint = function(s, w, h)
draw.RoundedBox(5, 0, 0, w, h, Color(0,0,0))
draw.RoundedBox(5, 2, 2, w-4, h-4, Color(50, 50, 50))
end

local DOAT2 = vgui.Create( “DLabel” , DOAP )
DOAT2:SetPos( 465, 20 )
DOAT2:SetText( “Dead or Alive” )
DOAT2:SetSize(200, 20)

local DOAB = vgui.Create( “DButton” , DOAP )
DOAB:SetPos( 977, 3)
DOAB:SetText( “X” )
DOAB:SetSize( 20, 20 )
DOAB:SetTextColor( Color(255,255,255))
DOAB.Paint = function(s, w, h)
draw.RoundedBox(3,0,0,w,h,Color(255, 50, 50))
end
DOAB.DoClick = function()
DOAP:Close()
end

local DOAC = vgui.Create( “DComboBox” , DOAP )
DOAC:SetPos( 400, 150 )
DOAC:SetSize( 200, 20 )
DOAC:SetValue( “Player” )
for k, v in pairs( player.GetAll() ) do
DOAC:AddChoice ( tostring( v:Name() ) )
end
DOAC:GetSelected(play)

local DOAT = vgui.Create( “DLabel” , DOAP )
DOAT:SetPos( 460, 35 )
DOAT:SetText( “Wanted menu” )
DOAT:SetSize(200,200)

local DOAB2 = vgui.Create( “DButton” , DOAP )
DOAB2:SetPos( 390, 180)
DOAB2:SetText( “Wanted” )
DOAB2:SetSize( 100, 20 )
DOAB2:SetTextColor( Color(255,255,255))
DOAB2.Paint = function(s, w, h)
draw.RoundedBox(3,0,0,w,h,Color(255, 50, 50))
end
DOAB2.DoClick = function()
wanted = true
unwanted = false
net.Start( “DOAMESS” )
net.WriteString( play )
net.WriteBool( unwanted )
net.WriteBool( wanted )
net.WriteBool( Deadoralive )
net.WriteBool( Dead )
net.WriteBool( Alived )
net.WriteFloat( Pricealive )
net.WriteFloat( Pricedead )
net.SendToServer()
end

local DOAB3 = vgui.Create( “DButton” , DOAP )
DOAB3:SetPos( 510, 180)
DOAB3:SetText( “Unwanted” )
DOAB3:SetTextColor(Color(255,255,255))
DOAB3:SetSize( 100, 20 )
DOAB3:SetTextColor( Color(255,255,255))
DOAB3.Paint = function(s, w, h)
draw.RoundedBox(3,0,0,w,h,Color(50, 180, 50))
end
DOAB3.DoClick = function()
unwanted = true
wanted = false
net.Start( “DOAMESS” )
net.WriteString( play )
net.WriteBool( unwanted )
net.WriteBool( wanted )
net.WriteBool( Deadoralive )
net.WriteBool( Dead )
net.WriteBool( Alived )
net.WriteFloat( Pricealive )
net.WriteFloat( Pricedead )
net.SendToServer()
end

DOACH = vgui.Create(“DCheckBox”, DOAP)
DOACH:SetPos(495,250)
DOACH:Toggle()
function DOACH:OnChange( bVal )
if ( bVal ) then
DOACH2:SetValue()
DOACH3:SetValue()
Deadoralive = true
end
end

DOACH2 = vgui.Create(“DCheckBox”, DOAP)
DOACH2:SetPos(145,250)
function DOACH2:OnChange( bVal )
if ( bVal ) then
DOACH:SetValue()
DOACH3:SetValue()
Dead = true
end
end

DOACH3 = vgui.Create(“DCheckBox”, DOAP)
DOACH3:SetPos(845,250)
function DOACH3:OnChange( bVal )
if ( bVal ) then
DOACH2:SetValue()
DOACH:SetValue()
Alived = true
end
end

local DOAT3 = vgui.Create( “DLabel” , DOAP )
DOAT3:SetPos( 140, 230 )
DOAT3:SetText( “Dead” )
DOAT3:SetSize(200, 20)

local DOAT4 = vgui.Create( “DLabel” , DOAP )
DOAT4:SetPos(475, 230)
DOAT4:SetText( “Dead or Alive” )
DOAT4:SetSize(200, 20)

local DOAT5 = vgui.Create( “DLabel” , DOAP )
DOAT5:SetPos( 840, 230 )
DOAT5:SetText( “Alive” )
DOAT5:SetSize(200, 20)

local DOATEXTE = vgui.Create( “DTextEntry”, DOAP )
DOATEXTE:SetPos( 410, 360 )
DOATEXTE:SetSize( 200, 20 )
DOATEXTE:SetText( “5000” )
DOATEXTE.OnEnter = function( self )
Pricedead = tonumber( self:GetValue() )
end

local DOATEXTE2 = vgui.Create( “DTextEntry”, DOAP )
DOATEXTE2:SetPos( 410, 420 )
DOATEXTE2:SetSize( 200, 20 )
DOATEXTE2:SetText( “5000” )
DOATEXTE2.OnEnter = function( self )
Pricealive = tonumber( self:GetValue() )
end

–local DOAB4 = vgui.Create( “DButton” , DOAP )
–DOAB4:SetPos( 460, 460)
–DOAB4:SetText( “Done” )
–DOAB4:SetTextColor(Color(255,255,255))
–DOAB4:SetSize( 100, 20 )
–DOAB4:SetTextColor( Color(255,255,255))
–DOAB4.Paint = function(s, w, h)
–draw.RoundedBox(3,0,0,w,h,Color(50, 50, 255))
–end
–DOAB3.DoClick = function()
– net.Start( “DOAMESS” )
– net.WriteString( play )
– net.WriteBool( unwanted )
– net.WriteBool( wanted )
– net.WriteBool( Deadoralive )
– net.WriteBool( Dead )
– net.WriteBool( Alived )
– net.WriteFloat( Pricealive )
– net.WriteFloat( Pricedead )
– net.SendToServer()
–end

local DOAT6 = vgui.Create( “DLabel” , DOAP )
DOAT6:SetPos( 485, 340 )
DOAT6:SetText( “Price dead” )
DOAT6:SetSize(200, 20)

local DOAT7 = vgui.Create( “DLabel” , DOAP )
DOAT7:SetPos( 485, 400 )
DOAT7:SetText( “Price alive” )
DOAT7:SetSize(200, 20)
return true
end

end )
[/Lua]

SERVER

[Lua]
util.AddNetworkString( “DOAMESS” )
net.Receive( “DOAMESS”, function(len, ply)
MsgN(“DOAMESS: Contains “…len…” bits”)
MsgN("Player: "…ply)

MsgN(play)
MsgN(net.ReadString())  

MsgN(unwanted)
MsgN(net.ReadBool())  

MsgN(wanted)
MsgN(net.ReadBool())

MsgN(doa)
MsgN(net.ReadBool())  

MsgN(dead)
MsgN(net.ReadBool())

MsgN(alive)
MsgN(net.ReadBool())

MsgN(pricealive)
MsgN(net.ReadFloat())

MsgN(pricedead)
MsgN(net.ReadFloat())

end)

if doa == true then
reason = “Dead or Alive”
end

if dead == true then
reason = “Dead”
end

if alive == true then
reason = “Alive”
end

print(play " is wanted by the police! Bring him " … reason " to the pd and you will get " … pricealive " if he is alive and " … pricedead " if he is dead!")

if wanted == true then
if play:isWanted() then return end
play:wanted( “Goverment”, play:Nick() … " is wanted by the police! " … reason … pricedead … pricealive, 120 )
end
end

if unwanted == true then
if not play:isWanted() then return end
play:unWanted(“Goverment”)
end
end
[/Lua]

[editline]28th March 2016[/editline]

Btw the buttons are functional and the code for the button is right, the button code here looks only a bit fake.

[editline]28th March 2016[/editline]

[Lua]local DOAC = vgui.Create( “DComboBox” , DOAP )
DOAC:SetPos( 400, 150 )
DOAC:SetSize( 200, 20 )
DOAC:SetValue( “Player” )
for k, v in pairs( player.GetAll() ) do
DOAC:AddChoice ( tostring( v:Name() ) )
end
DOAC:GetSelected(v)
play = “v”

wanted = true
unwanted = false
net.Start( "DOAMESS" )
	net.WriteString( play )
	net.WriteBool( unwanted )
	net.WriteBool( wanted )
	net.WriteBool( Deadoralive )
	net.WriteBool( Dead )
	net.WriteBool( Alived )
	net.WriteFloat( Pricealive )
	net.WriteFloat( Pricedead )
net.SendToServer()

[/Lua]

What is wrong here? It says error at net.WriteString( play ) because a string is expected and it prints a nil value? Pls help

[editline]28th March 2016[/editline]

I managed it to fix the errors but now I got another problem. When I send the network to the server it won’t do an action it just prints values in the console.

[Lua]
util.AddNetworkString( “DOAMESS” )
net.Receive( “DOAMESS”, function(len, ply)
MsgN(“DOAMESS: Contains “…len…” bits”)
–MsgN("Player: "…ply)

MsgN(play)
MsgN(net.ReadString())  

MsgN(unwanted)
MsgN(net.ReadBool())  

MsgN(wanted)
MsgN(net.ReadBool())

MsgN(doa)
MsgN(net.ReadBool())  

MsgN(dead)
MsgN(net.ReadBool())

MsgN(alive)
MsgN(net.ReadBool())

MsgN(pricealive)
MsgN(net.ReadFloat())

MsgN(pricedead)
MsgN(net.ReadFloat())

end)

if doa == true then
reason = “Dead or Alive”
end

if dead == true then
reason = “Dead”
end

if alive == true then
reason = “Alive”
end

print(play … " is wanted by the police! Bring him " … reason " to the pd and you will get " … pricealive " if he is alive and " … pricedead " if he is dead!")

if wanted == true then
if play:isWanted() then return end
play:wanted( “Goverment”, play:Nick() … " is wanted by the police! " … reason … pricedead … pricealive, 120 )
end

if unwanted == true then
if not play:isWanted() then return end
play:unWanted(“Goverment”)
end
[/Lua]

Values

[Lua]
DOAMESS: Contains 301 bits
nil
:heart: Dagun | kickback.com :heart:
nil
false
nil
true
nil
true
nil
true
nil
true
nil
5000
nil
5000

[/Lua]