Garry's Mod Wiki

Revision Difference

GM:PlayerInitialSpawn#527659

<function name="PlayerInitialSpawn" parent="GM" type="hook"> <ishook>yes</ishook> <description> Called when the player spawns for the first time. See <page>GM:PlayerSpawn</page> for a hook called every player spawn. <note>This hook is called before the player has fully loaded, when the player is still in seeing the "Starting Lua" screen. For example, trying to use the <page>Entity:GetModel</page> function will return the default model ("models/player.mdl")</note> <warning>Due to the above note, sending <page>net</page> messages to the spawned player in this hook is highly unreliable, and they most likely won't be received. See https://github.com/Facepunch/garrysmod-requests/issues/718. ⤶ The current hack is to create a <page>GM:InitPostEntity</page> hook on the client⤶ <warning>Due to the above note, sending <page>net</page> messages to the spawned player in this hook are highly unreliable, and they most likely won't be received. See https://github.com/Facepunch/garrysmod-requests/issues/718. ⤶ Current dirty hack without networking:⤶ ```⤶ hook.Add( "PlayerInitialSpawn", "FullLoadSetup", function( ply )⤶ hook.Add( "SetupMove", ply, function(self, ply, _, cmd )⤶ if self == ply and not cmd:IsForced() then hook.Run( "PlayerFullLoad", self ) hook.Remove( "SetupMove", self ) end⤶ end )⤶ end )⤶ ```⤶ ⤶ With networking:⤶ ``` hook.Add( "InitPostEntity", function()⤶ -- CLIENT⤶ hook.Add( "InitPostEntity", "Ready", function()⤶ net.Start( "cool_addon_client_ready" ) net.SendToServer() end ) ``` and then do what you need once the message is received by the server.⤶ ``` -- SERVER⤶ util.AddNetworkString( "cool_addon_client_ready" ) net.Receive( "cool_addon_client_ready", function( len, ply ) --send what you need here! end ) ```⤶ Alternatively, a server sided command can be run on the client if you wish to not use <page>net</page> messages.⤶ ⤶ Outdated dirty hack, left here so you can know what the purpose of this hack is.⤶ ``` hook.Add( "PlayerInitialSpawn", "FullLoadSetup", function( ply )⤶ hook.Add( "SetupMove", ply, function(self, ply, _, cmd )⤶ if self == ply and not cmd:IsForced() then hook.Run( "PlayerFullLoad", self ) hook.Remove( "SetupMove", self ) end⤶ end )⤶ end )⤶ ```</warning>⤶ </warning>⤶ </description> <realm>Server</realm> <predicted>No</predicted> <args> <arg name="player" type="Player">The player who spawned.</arg> <arg name="transition" type="boolean">If true, the player just spawned from a map transition.</arg> </args> </function> <example> <description>Prints the name of the player joining.</description> <code> function GM:PlayerInitialSpawn(ply) print( ply:GetName().." joined the server.\n" ) end -- That way you are overriding the default hook -- you can use hook.Add to make more functions get called when this event occurs local function spawn(ply) print( ply:GetName().." joined the game.\n") end hook.Add( "PlayerInitialSpawn", "some_unique_name", spawn ) </code> <output>Player1 joined the game</output> </example>