Revision Difference
GM:PlayerInitialSpawn#548511
<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>
<note>You can send <page>net</page> messages starting from the player_activate event (see <page>Game_Events</page>).</note>
<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 (more information here: https://github.com/Facepunch/garrysmod-requests/issues/718).
Workaround without networking:
```
hook.Add( "PlayerInitialSpawn", "FullLoadSetup", function( ply )
hook.Add( "SetupMove", ply, function( self, pl, _, cmd )
if self == pl and not cmd:IsForced() then⤶
hook.Run( "PlayerFullLoad", self )
hook.Remove( "SetupMove", self )
end⤶
end )⤶
end )
local load_queue = {}
⤶
hook.Add("PlayerInitialSpawn", "myAddonName/Load", function(ply)
load_queue[ply] = true⤶
end)
⤶
hook.Add("SetupMove", "myAddonName/Load", function(ply, _, cmd)
if load_queue[ply] and not cmd:IsForced() then⤶
load_queue[ply] = nil⤶
⤶
myAddon:OnPlayerNetReady(ply) -- Send what you need here!
end⤶
end)⤶
```
⤶
**Please note** that if addons are copy-pasting this bunch of code and editing the hook.Run to launch their own custom hook, only the first SetupMove launched will be executed, as the hook.Remove instruction will remove ALL the other hooks with the player identifier.⤶
Due to that, it is better practice to copy-paste this bunch of code without editing it and then add your hook to the ``PlayerFullLoad`` event name, instead of making custom edits that could potentially never be executed for either your addon or that of others.⤶
⤶
With networking:⤶
⤶
⤶
With networking:⤶
```
-- CLIENT
hook.Add( "InitPostEntity", "Ready", function()
net.Start( "cool_addon_client_ready" )
net.SendToServer()
end )
```
```
-- SERVER
util.AddNetworkString( "cool_addon_client_ready" )
net.Receive( "cool_addon_client_ready", function( len, ply )
-- Send what you need here!
end )
```
</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:Nick() .. " joined the server." )
end
-- That way you are overriding the default hook.
-- You can use hook.Add to make more functions get called when this event occurs.
hook.Add( "PlayerInitialSpawn", "some_unique_name", function( ply )
print( ply:Nick() .." joined the game." )
end)
</code>
<output>
```
Player1 joined the game.
```
</output>
</example>