Set a string to a certain player

Dont know how to word this but how would i set a string to the sender

What is meant to happen:

The player selects a weapon from the menu, this is then saved to the player and then when PlayerSpawn hook is called it will give the player the guns that he selected from the menu.



-- Server Side
if ( SERVER ) then 

   util.AddNetworkString( "PGun" ) 
   
   net.Receive( "PGun", function( len, ply )
		local PGun = net.ReadString()
                -- Set the players gun here but do NOT give it now.

   end )
   
end

if ( CLIENT ) then

-- Have derma buttons etc to send the data to the server
       net.Start( "PGun" )
           net.WriteString( gun selected from the client side goes here , i will just use local variable here. )
       net.SendToServer()	
end



So my question is:

How would i make it so the the server knows what gun the player selected?
Should be able to do the rest my self :slight_smile:

ply.SelectedGun = net.ReadString()

Use the PlayerLoadout hook instead of PlayerSpawn.

Hmm, it seems it is not sending it / setting it properly as im getting a nil value when i send the data.



if ( SERVER ) then 

   util.AddNetworkString( "PGun" ) 
   
   net.Receive( "PGun", function( len, ply )
	ply.SelectedGun = net.ReadString()
   end )
   
end

function PlayerSpawnClasses(ply)
	ply:Give(SelectedGun)
end
hook.Add("PlayerSpawn","PlayerSpawnClass",PlayerSpawnClasses)


The client side code is a tad too long, but i know that the net.Writestring works however its not being applied to SelectedGun

I apologise in advanced but i have not worked with using the method you are using before so im rather helpless.

It would be ply:Give(ply.SelectedGun)

You should add an if statement to check if they have selected any gun, though. If they haven’t selected one, it will error on spawn.

Ok, i have it all working now thanks :stuck_out_tongue:




function PlayerSpawnClasses(ply)
if ply.SelectedGun == nil then
	ply:Give("fas2_glock20")
	else
	ply:Give(ply.SelectedGun)
	end
end
hook.Add("PlayerSpawn","PlayerSpawnClass",PlayerSpawnClasses)




That worked fine. thanks! :smiley:

For a little cleaner looking code, you could do something like this:



ply:Give( ply.SelectedGun or "fas2_glock20" )


The other option is to initialize SelectedGun to a default value upon player initial spawn

Ah cool, learn something new every hour.

Thanks very much for the help guys, Solved.