Esx_dmvschool => add license in player inventory


#1

Hello !

I allow myself to post here because I can not add (once obtaining the permit), an item in the inventory of the player.

I used esx_dmvschool.

I have created in server/main.lua a register (like this) :

RegisterServerEvent('esx_dmvschool:addItem')
AddEventHandler('esx_dmvschool:addItem', function(item, count)
    local xPlayer  = ESX.GetPlayerFromId(_source)
		if xPlayer ~= nil then
			xPlayer.addInventoryItem('code', 1)
		end
end)

and in client/main.lua :

function StopTheoryTest(success)
	CurrentTest = nil

	SendNUIMessage({
		openQuestion = false
	})

	SetNuiFocus(false)

	if success then
		TriggerServerEvent('esx_dmvschool:addLicense', 'dmv')
		ESX.ShowNotification(_U('passed_test'))
		TriggerServerEvent('esx_dmvschool:addItem', 'code')
	else
		ESX.ShowNotification(_U('failed_test'))
	end
end

But i obtain an error ;

Error running call reference function for resource es_extended: citizen:/scripting/lua/scheduler.lua:351: server/classes/player.lua:321: attempt to index a nil value (local 'item')
stack traceback:
        server/classes/player.lua:321: in upvalue 'ref'
        citizen:/scripting/lua/scheduler.lua:337: in function <citizen:/scripting/lua/scheduler.lua:336>
        [C]: in function 'xpcall'
        citizen:/scripting/lua/scheduler.lua:336: in function <citizen:/scripting/lua/scheduler.lua:335>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:351: in function <citizen:/scripting/lua/scheduler.lua:322>
Error running system event handling function for resource esx_license: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: citizen:/scripting/lua/MessagePack.lua:830: missing bytes
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/MessagePack.lua:830: in method 'underflow'
        citizen:/scripting/lua/MessagePack.lua:465: in field 'any'
        citizen:/scripting/lua/MessagePack.lua:860: in field 'unpack'
        citizen:/scripting/lua/scheduler.lua:563: in field 'addInventoryItem'
        server/main.lua:21: in function 'AddLicense'
        server/main.lua:169: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:175: in function <citizen:/scripting/lua/scheduler.lua:174>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:41: in field 'CreateThreadNow'
        citizen:/scripting/lua/scheduler.lua:174: in function <citizen:/scripting/lua/scheduler.lua:138>

Item “code” is present in the base (table items and user_inventory).

Comment dois-je ajouter toutes les licences obtenues dans l’inventaire du joueur?

Thanks on advance :slight_smile:


#2

Hummm … Solved, i had forgetten “local _source = source”…

Sorry :smiley:


#4

can u upload the esx_license with an item?


#5

Hi !
main.lua => client :

function StopTheoryTest(success)
	CurrentTest = nil

	SendNUIMessage({
		openQuestion = false
	})

	SetNuiFocus(false)

	if success then
		TriggerServerEvent('esx_dmvschool:addLicense', 'dmv')
		ESX.ShowNotification(_U('passed_test'))
		TriggerServerEvent('esx_dmvschool:addItem', 'code_route')
	else
		ESX.ShowNotification(_U('failed_test'))
	end
end

function StartDriveTest(type)
		ESX.Game.SpawnVehicle(Config.VehicleModels[type], Config.Zones.VehicleSpawnPoint.Pos, 317.0, function(vehicle)
		CurrentTest       = 'drive'
		CurrentTestType   = type
		CurrentCheckPoint = 0
		LastCheckPoint    = -1
		CurrentZoneType   = 'residence'
		DriveErrors       = 0
		IsAboveSpeedLimit = false
		CurrentVehicle    = vehicle
		LastVehicleHealth = GetEntityHealth(vehicle)

		local playerPed   = GetPlayerPed(-1)
		TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
	end)

	TriggerServerEvent('esx_dmvschool:pay', Config.Prices[type])
end

function StopDriveTest(success)
	if success then
		TriggerServerEvent('esx_dmvschool:addLicense', CurrentTestType)
		ESX.ShowNotification(_U('passed_test'))
		if CurrentTestType == 'drive' then
			TriggerServerEvent('esx_dmvschool:addItemB', 'permis_b')
		elseif CurrentTestType == 'drive_bike' then
			TriggerServerEvent('esx_dmvschool:addItemA', 'permis_a')
		elseif CurrentTestType == 'drive_truck' then
			TriggerServerEvent('esx_dmvschool:addItemC', 'permis_c')
		end
		
	else
		ESX.ShowNotification(_U('failed_test'))
	end

	CurrentTest     = nil
	CurrentTestType = nil
end

function SetCurrentZoneType(type)
CurrentZoneType = type
end

main.lua => server

RegisterServerEvent('esx_dmvschool:addItem')
AddEventHandler('esx_dmvschool:addItem', function(item, count)
	local _source = source
    local xPlayer  = ESX.GetPlayerFromId(_source)
		if xPlayer ~= nil then
			xPlayer.addInventoryItem('code_route', 1)
		end
end)

RegisterServerEvent('esx_dmvschool:addItemA')
AddEventHandler('esx_dmvschool:addItemA', function(item, count)
	local _source = source
    local xPlayer  = ESX.GetPlayerFromId(_source)
		if xPlayer ~= nil then
			xPlayer.addInventoryItem('permis_a', 1)
		end
end)

RegisterServerEvent('esx_dmvschool:addItemB')
AddEventHandler('esx_dmvschool:addItemB', function(item, count)
	local _source = source
    local xPlayer  = ESX.GetPlayerFromId(_source)
		if xPlayer ~= nil then
			xPlayer.addInventoryItem('permis_b', 1)
		end
end)

RegisterServerEvent('esx_dmvschool:addItemC')
AddEventHandler('esx_dmvschool:addItemC', function(item, count)
	local _source = source
    local xPlayer  = ESX.GetPlayerFromId(_source)
		if xPlayer ~= nil then
			xPlayer.addInventoryItem('permis_c', 1)
		end
end)

Database :

INSERT INTO `items` (`name`, `label`, `limit`, `rare`, `can_remove`) VALUES
('code_route', 'Code de la route', 1, 0, 1),
('permis_a', 'Permis A', 1, 0, 1),
('permis_b', 'Permis B', 1, 0, 1),
('permis_c', 'Permis C', 1, 0, 1);

Or download : esx_dmvschool.zip (26.2 KB)