How to change a command in chat for a key pressed?

I want to put that /atm command to E button.

RegisterCommand('atm', function()
	local playerPed = PlayerPedId()
	local playerCoords = GetEntityCoords(playerPed, true)

	for i = 1, #Config.ATM do
        local atm = GetClosestObjectOfType(playerCoords.x, playerCoords.y, playerCoords.z, 1.0, Config.ATM[i], false, false, false)
        local atmPos = GetEntityCoords(atm)
        local dist = GetDistanceBetweenCoords(playerCoords.x, playerCoords.y, playerCoords.z, atmPos.x, atmPos.y, atmPos.z, true)
        if dist < 1.5 then
            openATM()
        end
    end
end)
Citizen.CreateThread(function() -- Whe create a thread
    while true do -- infinite loop that every 5 ms is executed
        -- 38 is the control of the E button
        if IsControlJustPressed(0, 38) then
            ExecuteCommand("atm")
        end
        Citizen.Wait(5)
    end
end)

Whit my library

IsControlJustPressed("E", function()
    ExecuteCommand("atm")
end)

bro 0.20 ms xdd

0.20 ms?

For 0.00 or 0.01ms in client-side use “RegisterKeyMapping(‘atm’, ‘Open ATM Menu’, ‘keyboard’, ‘E’)”

1 Like

you don’t need to use a loop xd use key mapping for more optimization

ik, but everyone use the IsControlJustPressed

The fact that “everyone use X native” doesn’t mean that it’s the best option. NekixYT is right - using keymaps is a significantly “lighter” option here and should definitely be used wherever possible.

i know, in fact i always use them, they should become a standard, but few in the forum know it

bro sorry for my bad vocabulary, i fell bad. :c

Citizen.CreateThread(function()
    while true do
        local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        local letSleep = true
        for i = 1, #Config.ATM do
            local atm = GetClosestObjectOfType(playerCoords.x, playerCoords.y, playerCoords.z, 1.0, Config.ATM[i], false, false, false)
            local atmPos = GetEntityCoords(atm)
            local dist = GetDistanceBetweenCoords(playerCoords.x, playerCoords.y, playerCoords.z, atmPos.x, atmPos.y, atmPos.z, true)
            if dist < 10.0 then
                letSleep = false
                if dist < 1.5 then
                    ShowHelpText('Press ~INPUT_PICKUP~ to access to the atm')
                    if IsControlJustPressed(0, 38) then
                        openATM()
                    end
                end
                break
            end
        end
        Citizen.Wait(letSleep and 500 or 5)
    end
end)

function ShowHelpText(text)
    AddTextEntry('HelpText', text)
    BeginTextCommandDisplayHelp('HelpText')
	EndTextCommandDisplayHelp(0, false, true, -1)
end