Handle the spawn of players

Hello everyone,
I’m new in FiveM developement (and in Lua). This is why I would ask you if I handle right my problem.

I want to handle by myself the spawning of a player in the server. The end goal would be to teleport him to his last position when he left the server.

I used esx.

This is what I want to try on the client side:

ESX                           = nil
local PlayerData              = {}

Citizen.CreateThread(function ()
    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
        Citizen.Wait(1)
    end

    while ESX.GetPlayerData() == nil do
        Citizen.Wait(10)
    end

    PlayerData = ESX.GetPlayerData()

    LoadMarkers()
end) 

AddEventHandler('es:playerLoaded', function(source, user)
	TriggerEvent("chatMessage", "", { 0, 0, 0 }, "Player loaded!")
	ESX.ShowNotification("Vous venez de vous connecté-e! votre dernière position est:" .. user.lastPosition)
	TriggerEvent("chatMessage", "", { 0, 0, 0 }, "2 -> Player last position :" .. PlayerData.lastPosition )
	--[[TriggerEvent('esx:teleport', playerData.lastPosition)]]--
end)

First part is variable initialization. Then I create a thread to get the playerData from ESX and then I listen to the es:playerLoaded event. When the event is triggered I show notification and chat message for debug purpose. This is where I would like to use the triggerEvent “esx:teleport”.

Do you think it’s the right way to handle this?

Thank you for your time and your help!

Dan

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.