Gcphone in item - HalCroves/gcphone GitHub Wiki

Welcome to the gcphone wiki!

Gcphone with item phone :

  1. Add in your data base :
INSERT INTO `items` (`name`, `label`, `limit`) VALUES  
    ('phone', 'Téléphone', 1)
;
INSERT INTO shops (id, name, item, price) VALUES (98, 'TwentyFourSeven', 'phone', 175), (99, 'RobsLiquor', 'phone', 175), (100, 'LTDgasoline', 'phone', 175);

Add on top of your client/main.lua :

ESX              = nil

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

Now in client/main.lua, search :

Citizen.CreateThread(function()

        while true do
            Citizen.Wait(0)
            if IsControlJustPressed(1, KeyOpenClose) and GetLastInputMethod( 0 ) then
                TooglePhone()
            end
            if menuIsOpen == true then
                for _, value in ipairs(KeyToucheCloseEvent) do
                    if IsControlJustPressed(1, value.code) then
                        SendNUIMessage({keyUp = value.event})
                    end
                end
            end
        end
end)

And replace by :

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		if IsControlJustPressed(1, KeyOpenClose) and GetLastInputMethod( 0 ) then
			ESX.TriggerServerCallback('gcphone:getItemAmount', function(qtty)
				if qtty > 0 then
					TooglePhone()
				else
					TriggerEvent('esx:showNotification', "Vous n'avez pas de ~r~téléphone~s~")
				end
			end, 'phone')
		end
		if menuIsOpen == true then
			for _, value in ipairs(KeyToucheCloseEvent) do
				if IsControlJustPressed(1, value.code) then
					SendNUIMessage({keyUp = value.event})
				end
			end
		end
	end
end)
  1. On your server/main.lua add :
ESX = nil

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

ESX.RegisterServerCallback('gcphone:getItemAmount', function(source, cb, item)
    local xPlayer = ESX.GetPlayerFromId(source)
    local qtty = xPlayer.getInventoryItem(item).count
    cb(qtty)
end)