配置esx_addonaccount报错求助:FiveM服务器重启后功能失效
Hey there, let's break down this error and get your esx_addonaccount working again. The core issue here is that the script is trying to access an xPlayer object that doesn't exist (it's nil) at line 87 of the server-side main.lua. Here's how to troubleshoot and fix this step by step:
1. Verify ESX Framework Load Order
esx_addonaccount depends entirely on the ESX core (like es_extended) to function. If it loads before ESX is ready, it can't fetch valid xPlayer objects.
- Open your
server.cfgand ensurees_extendedis loaded beforeesx_addonaccount. Your load list should look something like this:ensure es_extended ensure esx_addonaccount - If you have other ESX dependencies (like
esx_identity), make sure those are loaded before both ESX core and esx_addonaccount too—some player data relies on these to initialize properly.
2. Check the xPlayer Retrieval Logic in esx_addonaccount
Head to @esx_addonaccount/server/main.lua and look at line 87 and the surrounding code. Most often, this error happens when the script mishandles the esx:playerLoaded event:
- The
esx:playerLoadedevent already passes a validxPlayerobject as the second parameter. If your code is manually fetching it withESX.GetPlayerFromId(playerId)instead of using the provided parameter, that can lead to nil values (especially if the player hasn't fully initialized yet). - Example of correct event handling:
AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) -- Use the xPlayer passed directly from the event local accounts = MySQL.Sync.fetchAll('SELECT * FROM addon_account_data WHERE identifier = @identifier', {['@identifier'] = xPlayer.identifier}) -- Rest of your code end) - If you see
local xPlayer = ESX.GetPlayerFromId(playerId)in this handler, replace it with the event-providedxPlayerparameter.
3. Double-Check Database Tables
Even if you think your database is fine, confirm the required tables for esx_addonaccount exist and have data:
- Run these SQL queries in your database tool (like phpMyAdmin):
-- Check if addon_accounts table exists and has entries SELECT * FROM addon_accounts; -- Check if player-specific addon account data exists SELECT * FROM addon_account_data WHERE identifier = 'YOUR_TEST_PLAYER_IDENTIFIER'; - If either table is missing, re-run the SQL script included with esx_addonaccount (usually in the
sqlfolder) to create them properly.
4. Refresh esx_addonaccount Files
Since you're working in a clean environment, corrupted or outdated files could be the culprit:
- Download the latest stable version of esx_addonaccount from the official ESX repository.
- Replace your existing esx_addonaccount folder with the fresh download, making sure to preserve any custom account configurations you had.
5. Add Debug Logs to Diagnose
To confirm exactly when the nil value occurs, add a quick debug print to the event handler:
AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) print('Player ' .. playerId .. ' loaded. xPlayer status:', xPlayer) if not xPlayer then print('ERROR: xPlayer is nil for player ' .. playerId) end -- Original code here end)
Start your server and check the console output—this will tell you if the esx:playerLoaded event is firing correctly and if xPlayer is actually being passed.
Following these steps should resolve the nil xPlayer error. The most common fixes are adjusting load order or correcting how the esx:playerLoaded event uses the provided xPlayer parameter.
内容的提问来源于stack exchange,提问作者Dans

