Item check before ATM holdup and mhacking

Hi! I am trying to implement mhacking to ATM holdup for ESX. With a little catch that allows to do this only if player have “hacking device” in his inventory.

Everything works, except with this item check, I don’t know how to call the mhacking trigger from server side (where should be decided, if hack will even begin).

Could anyone help me on this, please? I can trigger this from client side, but because I want to limit use of this, I guess I need to figure it out from server side of the script…

RegisterServerEvent('esx_atmholdup:rob')
AddEventHandler('esx_atmholdup:rob', function(robb)

    local _source = source
    local xPlayer = ESX.GetPlayerFromId(_source)
	local hack = xPlayer.getInventoryItem('hack')
    local xPlayers = ESX.GetPlayers()

    if Stores[robb] then

        local store = Stores[robb]

        if (os.time() - store.lastrobbed) < Config.TimerBeforeNewRob and store.lastrobbed ~= 0 then

            TriggerClientEvent('esx:showNotification', source, _U('already_robbed') .. (Config.TimerBeforeNewRob - (os.time() - store.lastrobbed)) .. _U('seconds'))
            return
        end


        local cops = 0
        for i=1, #xPlayers, 1 do
         local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
         if xPlayer.job.name == 'police' then
                cops = cops + 1
            end
        end


        if rob == false then
		
		  if hack.count >= 1 then

            if(cops >= Config.PoliceNumberRequired)then

                rob = true
                for i=1, #xPlayers, 1 do
                     local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
                     if xPlayer.job.name == 'police' then
                            TriggerClientEvent('esx:showNotification', xPlayers[i], _U('rob_in_prog') .. store.nameofstore)
                            TriggerClientEvent('esx_atmholdup:setblip', xPlayers[i], Stores[robb].position)
                    end
                end
                TriggerClientEvent('esx:showNotification', source, _U('started_to_rob') .. store.nameofstore .. _U('do_not_move'))
                TriggerClientEvent('esx:showNotification', source, _U('alarm_triggered'))
                TriggerClientEvent('esx:showNotification', source, _U('hold_pos'))
				TriggerClientEvent('esx_atmholdup:currentlyrobbing', source, robb)
                Stores[robb].lastrobbed = os.time()
                robbers[source] = robb
                local savedSource = source
                
                    if(robbers[savedSource])then
                        rob = false
                        TriggerClientEvent('esx_atmholdup:robberycomplete', savedSource, job)
                        if(xPlayer)then          
                            local xPlayers = ESX.GetPlayers()
                            for i=1, #xPlayers, 1 do
                                 local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
                                 if xPlayer.job.name == 'police' then
                                        TriggerClientEvent('esx:showNotification', xPlayers[i], _U('robbery_complete_at') .. store.nameofstore)
                                        TriggerClientEvent('esx_atmholdup:killblip', xPlayers[i])
                                end
                            end
                        end
                    end
				--TriggerEvent("mhacking:show")              
				--TriggerEvent("mhacking:start",1,30,mycb)
				--TriggerClientEvent('esx_atmholdup:starthack')
				true
            else
                TriggerClientEvent('esx:showNotification', source, _U('min_police') .. Config.PoliceNumberRequired .. _U('in_city'))
            end
			
		  else
			TriggerClientEvent('esx:showNotification', source, ('You do not have a hacking device!'))
		  end
        else
            TriggerClientEvent('esx:showNotification', source, _U('robbery_already'))
        end
    end
end)

There is also this function that worked on client side

function mycb(success, timeremaining)
	if success then
		print('Success with '..timeremaining..'s remaining.')
		TriggerEvent('mhacking:hide')
		TriggerServerEvent('esx_atmholdup:done')
		ESX.ShowNotification(_U('paycheck'))
	else
		print('Failure')
		TriggerEvent('mhacking:hide')
	end
end

Error I am getting in console

InvokeNative: execution failed: Argument at index 1 was null.
Error running system event handling function for resource esx_atmholdup: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: Execution of native 000000002f7a49e6 in script host failed.
stack traceback:
        [C]: in upvalue '_in'
        citizen:/scripting/lua/natives_server.lua:450: in function 'TriggerClientEventInternal'
        (...tail calls...)
        @esx_atmholdup/server/main.lua:111: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:219: in function <citizen:/scripting/lua/scheduler.lua:218>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:41: in field 'CreateThreadNow'
        citizen:/scripting/lua/scheduler.lua:218: in function <citizen:/scripting/lua/scheduler.lua:182>

Thank you in advance for all responses!

Take a look at how I call it from there, I did that almost 2 years ago tho.

hypr9gran.zip (83.5 KB)

Thank you! I really appreciate your example!

If you still haven’t got it let me know and I can take a look!

I will try to go through it, but I have to leave it for another weekend. Too busy with work again. Only weekends are for scripting, lol.

I will let you know then how I’ve ended up

Here is how I have done it for a bankrobbing script ive been working on for a while now.

server side:

RegisterServerEvent('bankrob:HasGotRaspDoor')
AddEventHandler('bankrob:HasGotRaspDoor', function()
    local source = source
    local xPlayer = ESX.GetPlayerFromId(source)
    local raspberrypi = xPlayer.getInventoryItem('raspberrypi')
    local xPlayers = ESX.GetPlayers()

    local cops = 0
    for i=1, #xPlayers, 1 do
     local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
     if xPlayer.job.name == 'police' then
            cops = cops + 1
        end
    end
    if xPlayer.getInventoryItem('raspberrypi').count <= 0 then
        TriggerClientEvent('esx:showNotification', source, '~h~~r~You don\'t have the correct equipment.')
    elseif (cops < Config.NumberOfCopsRequired)then
        TriggerClientEvent('esx:showNotification', source, '~h~~r~Not enough cops in town.')
    elseif (cops >= Config.NumberOfCopsRequired)then
        if xPlayer.getInventoryItem('raspberrypi').count >= 1 then
            TriggerClientEvent('bankrob:HasGotRaspDoor', source, GotRaspDoor)
        end
    end
end)

client side:

RegisterNetEvent('bankrob:HasGotRaspDoor')
AddEventHandler('bankrob:HasGotRaspDoor', function(GotRaspDoor)
	local playerPed = PlayerPedId()

		ESX.ShowNotification("hacking started")
		TaskGoStraightToCoord(playerPed, 261.56, 223.26, 106.287, 1.0, -1, 251.41, 0.0)
		IsBlocked = true
		Citizen.Wait(2500)
		TaskStartScenarioInPlace(playerPed, 'WORLD_HUMAN_STAND_MOBILE', 0, true)
		Citizen.Wait(1000)
		TriggerEvent("mhacking:show")
		TriggerEvent("mhacking:start",7,25,hacking)
		IsHackingDoor = true
end)

and this is also part of the client side that triggers the whole event:

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		local playerPed = PlayerPedId()
		local coords = GetEntityCoords(playerPed)

		if GetDistanceBetweenCoords(coords, 261.87, 223.13, 106.28, true) < 0.5 then
			if not IsHackingDoor and not IsDoorHacked then
				ESX.ShowHelpNotification('Press ~INPUT_CONTEXT~ to hack the door!')
			end
			if IsControlJustReleased(0, Keys['E']) and not IsHackingDoor then
				TriggerServerEvent('bankrob:HasGotRaspDoor')
			end
		end
	end
end)

Thank you a lot, @hypr9xa! I’ve managed to even get working that stupid sequential hack which I had so many troubles with :slight_smile:

Now it works like a charm!

Glad to hear that!

Have fun coding!

1 Like

posted to wrong thread.