Spawning the Player
alt:V works in an event driven manner. Which means that something must happen from an event before anything can happen. There will always be a cause
but you are mostly responsible for writing the effect
.
With that in mind, let's take a look at playerConnect
.
Because alt.on
is a function
we are going to execute that function from the main file. Anything that is written in your base file outside of a function
will automatically execute. This is why alt.log
shows Hello from server
in your console automatically.
Let's add the playerConnect
event. Type out alt.on('playerConnect')
and you will see some information popup about what it's expecting.
It is looking for something called a listener
and it has one single parameter
called player
with a type of alt.Player
. This means it's going to send us a player
when a player connects to the server.
Fat Arrow Example
Alow known as lambda, anonymous, or closure functions.
/// <reference types="@altv/types-server" />
import * as alt from 'alt-server';
alt.log('Hello from server');
alt.on('playerConnect', (player) => {
// Do something to the player.
});
This is one of many ways to handle an event. This uses a fat arrow function to create a new function inside of alt.on
.
Traditional Function Example
Here's an alternative way
to write the above connection string.
/// <reference types="@altv/types-server" />
import * as alt from 'alt-server';
alt.log('Hello from server');
alt.on('playerConnect', handleConnect);
/**
* @param {alt.Player} player
*/
function handleConnect(player) {
// Do something to the player.
}
This uses a normal function with a type definition in @param to help give auto-completion inside of our new function.
Which is Better?
Both are completely fine and valid in JavaScript. The document writer prefers the traditional method for the sake of readability and understanding code.
Add the Spawn Code
In order to spawn the player
we need to modify the player
we recieved from the event
. Simply type player.spawn
and you'll see that it is a function
and that it needs additional parameters. Here is what we ended up with in the server/startup.js
file.
/// <reference types="@altv/types-server" />
import * as alt from 'alt-server';
alt.log('Hello from server');
alt.on('playerConnect', handleConnect);
/**
* @param {alt.Player} player
*/
function handleConnect(player) {
// x, y, z, delay in ms
player.spawn(-1291.71, 83.43, 54.89, 1000); // Spawns after 1 second.
}
Add the Model Code
By default alt:V does not automatically spawn or give the player a model when they join the game. It's important to give them at least one of the two customizable models for GTA:Online when they join. Those models are mp_m_freemode_01
and mp_f_freemode_01
. They can be assigned to the .model
parameter of a player
.
/// <reference types="@altv/types-server" />
import * as alt from 'alt-server';
alt.log('Hello from server');
alt.on('playerConnect', handleConnect);
/**
* @param {alt.Player} player
*/
function handleConnect(player) {
player.spawn(-1291.71, 83.43, 54.89, 1000); // Spawns after 1 second.
player.model = `mp_m_freemode_01`;
}
Bootup your server from the command line, join or reconnect. Then see what happens. You should be spawned into the world and you will be free to move around.
Written by Stuyk & Alexa