Help with net.WriteString and various other simple issues.

Hello all I am working on a small script and I want to exspand my knowledge on using the net libraries. I was trying to do something I thought was rather simple until I relised I couldn’t do it.

Perhaps someone could help me and educate me at the same time. Here is my code.




client side:
 
LoadoutMenu3 = vgui.Create( "DImageButton", LoadoutMenu3 )
        LoadoutMenu3:SetParent( LoadoutMenu2 )
        LoadoutMenu3:SetPos( 40, 40 )                                                      
        LoadoutMenu3:SetImage( "AN IMAGE ON THE LOADOUT SCREEN" )        
        LoadoutMenu3:SizeToContents()    
        LoadoutMenu3.DoClick = function()
 
                net.Start( "testing1" )
                net.SendToServer()     
                ChangeClassClient (1)
                BlurMenu:Close()
                InvisiblePanel:Close()
                InvisiblePanel2:Close()
                LoadoutMenu:Close()
                LoadoutMenu2:Close()
                StatsMenu:Close()
        end
 
        LoadoutMenu4 = vgui.Create( "DImageButton", LoadoutMenu4 )
        LoadoutMenu4:SetParent( LoadoutMenu2 )
        LoadoutMenu4:SetPos( 40, 200 )                                                      
        LoadoutMenu4:SetImage( "ANOTHER IMAGE on the loadout screen" )        
        LoadoutMenu4:SizeToContents()
        LoadoutMenu4.DoClick = function()
       
                net.Start( "testing2" )
                net.SendToServer()     
               
        ChangeClassClient (1)
                BlurMenu:Close()
                InvisiblePanel:Close()
                InvisiblePanel2:Close()
                LoadoutMenu:Close()
                LoadoutMenu2:Close()
                StatsMenu:Close()
        end    
       
 
 
serverside:
 
                net.Receive( "testing1", function(len, pl)    
                pl:Message("The first test is working", 2, "white")
                end)
       
                net.Receive( "testing2", function(len, pl)
                pl:Message("The second test is working", 2, "white")
                end)
       
 
function GM:SpawnSuit( pl, hattype )
--This has code in it which spawns a hat on the player essentially. Its all custom with a shop system etc..
end    
 
I need to set a 'hat' on the player depending on which net message is received.
But I can't figure out how to do this. Moving the state of the net.Receive into  this function.
I just need to be able to do something like
 
if test1 then
-give this hat
elseif test2 then
-Give this hat
end
 
Obviously its a little more complex



I was told by someone that I could get this to work with using Read.String() and WriteString("") But I am totally confused on how to use them

Thanks for any help.

Is that your actual code, or just copy & pasted parts from different files? :v:

No my code. Client side is one file. Serverside is in the init.lua :L

  1. Player:Message - such function does not exist, unless you made it
  2. Make sure net.Receive is not inside any other functions or hooks.
  3. Use tabs instead of spaces to indent your code


net.Start( "testing2" )
    net.WriteString("Test, max length is 512")
net.SendToServer()

net.Receive( "testing2", function(len, pl)
    local msg = net.ReadString() -- You must read in the same order as you write.

    pl:Message( msg, 2, "white") -- Do not put net.ReadString() in the same net.Receive multiple times and expect it to return the same string.
end)


pl:Message(“The second test is working”, 2, “white”) is custom yes.

I see, well want I want to do basically is make it so if someone clicks on the image in the loadout screen it gives them a hat. I NEED the data to which is being transferred from the net.Receive to be in the function GM:SpawnSuit( pl, hattype )

But I can’t seem to do this. As I said above. Do you have any idea’s?

Thanks

Save the data to a table or onto the player object and access it from GM:SpawnSuit when needed. Don’t forget to add validity checks, etc.

How would I go about saving it to a table via a net.Receive also accessing it within the other function. I haven’t ever used it in this way before. Thus this script and thread. :slight_smile:

local myTable = {}

in the top of the file.

Then to save,

myTable[ ply:SteamID() ] = data

data can be a string or a table, you figure out what you need.

Then to access that data you do this:

local data = myTable[ ply:SteamID() ]