Attempted to index a nil value (global 'ESX')

I found some topics of this in the forums but no solution

ESX = nil

RegisterNetEvent('apvoge')
AddEventHandler('apvoge', function()
	local xPlayer = ESX.GetPlayerFromId(source)
	xPlayer.addMoney(500)
end)

This is the server sided script ^

			if robbingTime < 1 then
				robbing = false
				robbingTime = 0
				TriggerServerEvent('apvoge')
			else

This is the client sided script that is calling the ‘apvoge’ event

4 Likes

Are you somewhere defining ESX isntead of just letting it nil?

2 Likes

The reason you find no solutions is because that error can be caused by a myriad of issues. The first thing to check is to make sure that this resource is starting after es_extended.

There’s a lot of your script that isn’t being displayed so the issue could be the way the client file initiates ESX.

4 Likes

@TheIndra

Are you somewhere defining ESX isntead of just letting it nil?

I don’t really understand what you mean, but if you mean ESX = nil, then yes, in the server script , at top I use ESX = nil, nowhere else

@schwim

The reason you find no solutions is because that error can be caused by a myriad of issues. The first thing to check is to make sure that this resource is starting after es_extended.
There’s a lot of your script that isn’t being displayed so the issue could be the way the client file initiates ESX.

local markerCoords = vector3(-1223.373, -907.041, 12.326)

local blip = AddBlipForCoord(markerCoords.x, markerCoords.y)

local h_key = 74

local robbing = false
local robbingTime = 0

local color = {
	r=0,
	g=255,
	b=255,
	a=255
}

SetBlipSprite(blip, 59)
SetBlipDisplay(blip, 6)
SetBlipScale(blip, 0.9)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("Parduotuve")
EndTextCommandSetBlipName(blip)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1)
		DrawMarker(2, markerCoords.x, markerCoords.y, markerCoords.z, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 255, 255, 0, 255, true, false, 2, false, false, false, false)

		if Vdist2(GetEntityCoords(PlayerPedId(), false), markerCoords) < 1 then
			alert("Norint apiplešti parduotuvę spauskite ~INPUT_VEH_HEADLIGHT~")
			if IsControlJustReleased(1, h_key) then
				if robbing then
					notif("Jus jau apiplešinejate")
				else
					notif("Pradejote apiplešimą, jums reikia išbuti parduotuveje 2 minutes")
					robbing = true
					robbingTime = 5
				end
			end
		end

		if robbing then
			SetTextFont(0)
			SetTextScale(0.4, 0.4)
			SetTextColour(color.r, color.g, color.b, color.a)
			SetTextEntry("STRING")
			AddTextComponentString("Liko "..robbingTime.." sek.")
			DrawText(0.5, 0.5)
		end
	end
end)

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(1000)
		if robbing then
			if Vdist2(GetEntityCoords(PlayerPedId(), false), markerCoords) > 50 then
				notif("Išejote iš parduotuves, apiplešimas buvo nutrauktas")
				robbing = false
				robbingTime = 0
			end
			if robbingTime < 1 then
				robbing = false
				robbingTime = 0
				TriggerServerEvent('apvoge')
			else
				a = robbingTime - 1
				robbingTime = a
			end
		end
	end
end)

function alert(msg)
	SetTextComponentFormat("STRING")
	AddTextComponentString(msg)
	DisplayHelpTextFromStringLabel(0,0,1,-1)
end

function notif(msg)
	SetNotificationTextEntry("STRING")
	AddTextComponentString(msg)
	DrawNotification(true, false)	
end

Here’s the whole client script

EDIT: I also tried executing this resource after es_extended but that didn’t fix it

1 Like

Your problem(or at least one of them) is that you’re not initiating ESX. Put this at the top of your client file:

ESX								= nil

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

	Citizen.Wait(5000)
	PlayerData = ESX.GetPlayerData()
end)
9 Likes

I added this but it still gives me the attempt to index a nil… in the server script , not theclient

This should be at the top of your server file:

ESX = nil

TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
31 Likes

Finally , it works, thank you so much

9 Likes

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