OnPlayerConnected

From GTA Network Wiki
Jump to: navigation, search

OnPlayerConnected
Server-Side Function

utilities-terminal.png Server-Side csharp.png C#

This event is invoked once the player has finished loading all the required resources.

Attribute

[ServerEvent(Event.PlayerConnected)]

Required Parameters

  • Client: the player who connected.

Usage example(s)

[ServerEvent(Event.PlayerConnected)]
public void OnPlayerConnected(Client player)
{
   player.SendChatMessage("Welcome to our server!");
}

There are several ways to change the default spawning locations.


  • 1st method: Changing default spawn location.

This will simply change the default global player spawn location.

Note: the X, Y, Z and rotation are random values. :kappa:

[ServerEvent(Event.ResourceStart)]
public void OnResourceStart()
{
    NAPI.Server.SetDefaultSpawnLocation(new Vector3(1, 2, 3));
}

  • 2nd method: Handling spawning manually

Very useful for randomizing spawn locations.

We're gonna create a new OnPlayerConnected method and assign an Event Attribute to it to take over control of the server player connected event.

In order to disable the default spawn on connect behavior and have full control over the player connection event, add DisableDefaultOnConnectSpawn attribute on top of you player connect event method.

[DisableDefaultOnConnectSpawn]
[ServerEvent(Event.PlayerConnected)]
public void OnPlayerConnected(Client player)
{
    NAPI.Player.SpawnPlayer(player, new Vector3(2, 5, 8)); // Will spawn the player at the given position
}

Changelog

Version Description
1.0 Release.

References