Skip to main content

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.

In FiveM, the spawn bridge also shuts down the loading screen by default as part of the initial spawn flow.

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)

SpawnOptions supports:

  • appearance?: PlayerAppearance
  • skipLoadingScreenShutdown?: boolean

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!')
}
}

FiveM manual loading screen control

If your FiveM resource uses loadscreen_manual_shutdown 'yes', you can keep the loading screen visible until your scene is ready:

await this.spawnService.spawn(data.pos, data.model, data.heading, {
appearance: data.appearance,
skipLoadingScreenShutdown: true,
})

// Load assets, set up camera, prepare scene...
ShutdownLoadingScreenNui()

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 AppearanceService to apply clothing and facial features automatically during the spawn.
  • FiveM: skipLoadingScreenShutdown is primarily intended for FiveM cinematic intro and character selection flows. When set, your code becomes responsible for calling ShutdownLoadingScreenNui().