Spawn
Description
The SpawnService manages the complete lifecycle of a player joining the server, from the initial connection to the final fade-in when the character is ready to play.
Key Features
Robust Lifecycle Management
Spawning in FiveM is complex. The SpawnService handles:
- Waiting for the network session to be ready.
- Loading the player model.
- Ensuring the physical ped exists.
- Loading collisions at the spawn point (to prevent falling through the map).
- Resurrecting the local player cleanly.
Fade Transitions
The service automatically handles DoScreenFadeOut and DoScreenFadeIn during the spawn process, ensuring a smooth transition for the player.
API Methods
spawn()
Performs the initial spawn. Typically called once when the player first joins or selects a character.
async spawn(position: Vector3, model: string, heading?: number, options?: SpawnOptions)
respawn()
Used for resurrecting a dead player or moving them to a new location while maintaining their current model and state.
teleportTo()
A safe teleportation method that ensures collisions are loaded at the destination before moving the ped.
waitUntilSpawned()
A helper that allows other services or controllers to pause execution until the player has successfully completed their first spawn.
Example
@Controller()
export class SessionController {
constructor(private readonly spawnService: SpawnService) {}
@OnNet('core:client:spawn')
async handleInitialSpawn(data: any) {
await this.spawnService.spawn(data.pos, data.model, data.heading, {
appearance: data.appearance
})
console.log('Player is now live in the world!')
}
}
Notes
- Timeouts: Built-in timeouts for network, ped, and collision loading prevent the client from hanging indefinitely.
- Appearance: If an appearance object is provided, it uses
AppearanceServiceto apply clothing and facial features automatically during the spawn.