[HELP]Esx_extended - Esx.PlayerData is nil when disconnecting/reconnecting from server

Hi, everyone :grinning:

I’m new to cfx administration/script writing and i’m not too familiar with Lua (more of a python guy).

I’m simply trying to diplay the values of the players different money accounts (“bank”,“money”,“black_money”) in an UI.

To do so I was using the ESX object and is PlayerData attribute. When I start my ressource it works perfectly, I get the values of each account by looping through EXS.PlayerData.accounts but the issue appears when i disconnect/reconnect.
When a player reconnect to the server the EXS.PlayerData seems to be “nil” which make my script bug and not working until I restart the ressource.

Thanks !

CODE :

this happens for the lastest ESX version? (1.1 or 1.2)
My guess would be that on disconnect the PlayerData is deleted. Somewhere. And on reconnect it is not reloaded from DB. That would result in the Data being nil. But I never experienced such an issue.

Hi ! thank for the answer !

I am using ESX-legacy, so i guess it’s the 1.2 (It’s pretty hard to keep track of the ressources versions, with all the versionning and differents githubs/download posts that’s pop up everywhere hahaha).

You must be right, that means that I’ll need to find where exactly is the Event/Function that reloads the datas from the DB and asign it to PlayerData. I thought I found the Event that does that in ESX_extended, but doesn’t seems to have any issue in the code (that i can tell at least).

I am thinking it might be because I don’t start all the ESX ressources in my serv.cfg, I only start those I need for now. The thing is that I still start the core ressources (I assume), it’s only in the ESX_addons that are not all started.

I’ll try to start all the ESX_ressouces (core and addons) and run some tests on the ESX Object. I’ll get back to you then.

Have a nice day :slightly_smiling_face:

Hi, everyone !

I found a solution to my problem. I used the method ESX.GetPlayerData() instead of using the attribute itself ESX.PlayerData , I did try this before but it didn’t work because I was using it outside my looped Thread (beginner mistake I guess ^^’).

Here is my new working code :
screen3

But the lack of informations is still a little frustrating, I am still wondering why the attribute ESX.PlayerData is set when the ressource is started but become Nil when the player is deconnecting/reconnecting (or if he connects after the ressource was started) :thinking:

Client <> server

When the player (client) connects to the server the resource gets the initial data set by ESX, which has yet to be filled. How would ESX.PlayerData have initial values containing all the character information stored on the server?

ESX Legacy is 1.3, not 1.2 and while some of the newer features aren’t included on documentation there is plenty of information in the readme, which also directs you to look at esx_example.

RegisterNetEvent('esx:playerLoaded') -- Store the players data
AddEventHandler('esx:playerLoaded', function(xPlayer, isNew)
	ESX.PlayerData = xPlayer
	ESX.PlayerLoaded = true
end)

RegisterNetEvent('esx:playerLogout') -- When a player logs out (multicharacter), reset their data
AddEventHandler('esx:playerLogout', function(xPlayer, isNew)
	ESX.PlayerLoaded = false
	ESX.PlayerData = {}
end)

Always include those events in your resources that require ESX, and add the following to your fxmanifest (above any other file being loaded)

shared_script '@es_extended/imports.lua'

1 Like

Hey ! thanks for the answer !! :slightly_smiling_face:

Yes, I saw the Readme of esx_example (I digged a little bit every where :wink: , I went in a lot of the client/server lua script of the different esx_ressources).
As soon as I found this readme, I added the shared_script @es_extended/imports.lua’ and tryed to Trigger the Event ‘esx:playerLoaded’ in an attempt to set the PlayerData.

But as I am just starting to dabble with Cfx , I couldn’t wrap my head around, the different Events and when/how to trigger one or the other.

If you have a little time, could you tell me a little bit more about those two events (playerLoaded & playerLogout)

Ps: I wasn’t trying to speak badly about the ESX or the Cfx docs at all (I know It might have sound like it though :sweat_smile:). I was just lost, it being new to me, and I being use to masive docs.
But it seams to be a realy nice framework, I’ll continue to learn about it and hopfully be able to exploit it’s full potential.

esx:playerLoaded is triggered by the framework when it loads the data from the database and creates a player object, you shouldn’t be triggering it. esx:onPlayerLogout is triggered when you log out of a character while using esx_multicharacter and return to character selection.

Ok, thanks for the info.