Sharing PlayerInitialSpawn with Client

Hi,

I’m trying to replicate the server hook PlayerInitialSpawn clientside, here is what I have:



if ( SERVER ) then

	hook.Add( "PlayerInitialSpawn", "myscript_sv_hooks_PlayerInitialSpawn",
		function( ply )
			util.AddNetworkString( "myscript_cl_hooks_PlayerInitialSpawn" );
			net.Start( "myscript_cl_hooks_PlayerInitialSpawn" )
			net.Send( ply )
		end
	)

else

	net.Receive( "myscript_cl_hooks_PlayerInitialSpawn",
		function( len, ply )
			print( tostring( ply ) .. " " .. tostring( LocalPlayer() ) )
		end
	)

end


But this prints “nil [NULL Entity]”…

What can I do, is there a better way than what I’m trying to do? I would like to replicate many server hooks on the client, because I don’t understand why they aren’t shared :S

Thanks in advance.

You’re sending an empty message to the player who spawned and then you’re trying to use the 2nd parameter on a clientside net.Receive callback which doesn’t exist (that’s for when the client sends the server a message, it identifies who sent it).

**[Lua/Libraries/net/WriteEntity

http://gmodwiki.net/favicon.ico](http://gmodwiki.net/Lua/Libraries/net/WriteEntity)**

Also, put util.AddNetworkString outside the hook callback.

You don’t even need to send the player object, it will be nil on client anyway AFAIK ( Because LocalPlayer is nil, the entity for localplayer is not initialized yet. ), make a timer of 0 seconds and use LocalPlayer()

Thanks, but it’s still not working correctly:



if ( SERVER ) then

	util.AddNetworkString( "myscript_cl_hooks_PlayerInitialSpawn" );
	hook.Add( "PlayerInitialSpawn", "myscript_sv_hooks_PlayerInitialSpawn",
		function( ply )
			net.Start( "myscript_cl_hooks_PlayerInitialSpawn" )
			net.WriteEntity( ply )
			--net.Send( ply )
			net.Broadcast()
		end
	)

else

	net.Receive( "myscript_cl_hooks_PlayerInitialSpawn",
		function( len )
			print( tostring( net.ReadEntity() ) )
		end
	)

end


When I spawn on the server for the first time, it prints “[NULL Entity]”.
And when another player join the server and spawn for the first time, I see “Player [2][nick]”, while he will see “[NULL Entity]” :confused:

Edit: Ok Robotboy, I was hoping that I would not need to use a timer or some other crappy workaround that I used to use, years ago…

You can use the OnEntityCreated hook as well and check if it’s the LocalPlayer()

PlayerInitialSpawn is called during Sending Client Info, or just before. The player hasn’t fully initialized at this point. You could do a hack and use a timer, or what brandon says; when the entity is created - send the net message.

No need to send the player info, just send an empty net message to the client like so:

[lua]util.AddNetworkString( “ClientPlayerInitialSpawn” );
net.Start( “ClientPlayerInitialSpawn” );

// Send it to the connecting player, or if everyone needs to know about it
net.Send( ply );

// this is the or
net.Broadcast( );[/lua]

Do not use both Send and Broadcast, one or the other…

Thanks all :wink: