[HELP][FIVEM][LUA] - I need the menu when it's open, if I press "E" it closes

Hello, I would like when I press “e” the menu appears and when I press “e” the menu closes again. Another thing: I also wanted to, if possible, when the menu appears, the text (draw3DText) underneath hide and then appear again.

Citizen.CreateThread(function()
	SetNuiFocus(false,false)
	while true do
		Citizen.Wait(5)
		for _,mark in pairs(marcacoes) do
			local x,y,z = table.unpack(mark)
			local distance = GetDistanceBetweenCoords(GetEntityCoords(PlayerPedId()),x,y,z,true)
			if distance <= 0.5 then
				--DrawMarker(21,x,y,z-0.6,0,0,0,0.0,0,0,0.5,0.5,0.4,0,0,255,50,0,0,0,1)
				draw3DText(x, y, z, "PRESSIONE ~g~E~w~ PARA ACESSAR À ~y~MERCEARIA")
				--drawTxt("PRESSIONE  ~b~E~w~  PARA EMPACOTAR ENCOMENDA",4,0.5,0.93,0.50,255,255,255,180)
				if IsControlJustPressed(0,38) then                                                                  
					ToggleActionMenu()
				end
			end
		end
	end
end)
function draw3DText(x,y,z, text)
	local onScreen,_x,_y=World3dToScreen2d(x,y,z)
	local px,py,pz=table.unpack(GetGameplayCamCoords())
	SetTextScale(0.35, 0.35)
	SetTextFont(4)
	SetTextProportional(1)
	SetTextColour(255, 255, 255, 255)
	SetTextEntry("STRING")
	SetTextCentre(1)
	AddTextComponentString(text)
	DrawText(_x,_y)
	local factor = (string.len(text)) / 370
	DrawRect(_x,_y+0.0125, 0.01+ factor, 0.03, 0, 0, 0, 80)
end

Could you put the ToggleActionMenu() function here?

1 Like

sure

local menuactive = false
function ToggleActionMenu()
	menuactive = not menuactive
	if menuactive then
		SetNuiFocus(true,true)
		TransitionToBlurred(1000)
		SendNUIMessage({ showmenu = true })
	else
		SetNuiFocus(false)
		TransitionFromBlurred(1000)
		SendNUIMessage({ hidemenu = true })
	end
end

Try SetNUIFocus(false,true) instead of true,true

Not sure if this would work but give it a try

it even works, but the cursor and focus are “weird”

So change your original thread to this:

local ShowPressEText=true
Citizen.CreateThread(function()
	SetNuiFocus(false,false)
	while true do
		Citizen.Wait(5)
		for _,mark in pairs(marcacoes) do
			local x,y,z = table.unpack(mark)
			local distance = GetDistanceBetweenCoords(GetEntityCoords(PlayerPedId()),x,y,z,true)
			if distance <= 0.5 then
               if ShowPressEText then
				    --DrawMarker(21,x,y,z-0.6,0,0,0,0.0,0,0,0.5,0.5,0.4,0,0,255,50,0,0,0,1)
				     draw3DText(x, y, z, "PRESSIONE ~g~E~w~ PARA ACESSAR À ~y~MERCEARIA")
				     --drawTxt("PRESSIONE  ~b~E~w~  PARA EMPACOTAR ENCOMENDA",4,0.5,0.93,0.50,255,255,255,180)
                end
				if IsControlJustPressed(0,38) then                                                                  
					ToggleActionMenu()
				end
			end
		end
	end
end)

Then change the ToggleActionMenu() function to this:

local menuactive = false
function ToggleActionMenu()
	menuactive = not menuactive
	if menuactive then
        ShowPressEText=false
		SetNuiFocus(true,true)
		TransitionToBlurred(1000)
		SendNUIMessage({ showmenu = true })
	else
        ShowPressEText=true
		SetNuiFocus(false)
		TransitionFromBlurred(1000)
		SendNUIMessage({ hidemenu = true })
	end
end

it didn’t work, could you please check?

Mr @SpiderMonkeyIV or anyone who can help me, could you help me when I press “E” with the menu open, it closes the menu? Thanks!

local menuactive = false
function ToggleActionMenu()
	menuactive = not menuactive
	if menuactive then
		ShowPressEText=false
		SetNuiFocus(true,true)
		TransitionToBlurred(1000)
		SendNUIMessage({ showmenu = true })
	else
		ShowPressEText=true
		SetNuiFocus(false)
		TransitionFromBlurred(1000)
		SendNUIMessage({ hidemenu = true })
	end
end
Citizen.CreateThread(function()
	SetNuiFocus(false,false)
	ShowPressEText = true
	while true do
		Citizen.Wait(5)
		for _,mark in pairs(lojas) do
			local x,y,z = table.unpack(mark)
			local distance = GetDistanceBetweenCoords(GetEntityCoords(PlayerPedId()),x,y,z,true)
			if distance <= 0.5 then
               			if ShowPressEText then
					--DrawMarker(21,x,y,z-0.6,0,0,0,0.0,0,0,0.5,0.5,0.4,0,0,255,50,0,0,0,1)
					--draw3DText(x, y, z, "PRESSIONE ~g~E~w~ PARA ACESSAR A ~y~MERCEARIA")
					drawTxt("~y~PRESSIONE ~b~[E] ~w~PARA ACESSAR A ~b~MERCEARIA",4,0.075,0.97,0.40,255,255,255,180)
				end
				if IsControlJustPressed(0,38) then    
					ShowPressEText = false                                                              
					ToggleActionMenu()
				end
			end
		end
	end
end)

You do a listener for key press on the JS (browser) side, send a NUI callback and close your NUI.
Simple as that

To hide the drawtext, check if menu is open, if so don’t render your text.

There is function in jQuery

$(selector).on('keyup', function(event) {});

So you can put it in your javascript inside your ready function

$(document).on('keyup', function(event) {
    if (event.keyCode === 69) { //69 stands for E (https://keycode.info/)
        $.post(`https://${GetParentResourceName()}/close`, JSON.stringify({}));
    }
});

And then put this inside your client file

RegisterNUICallback('close', function(data, cb)
    cb(ToggleActionMenu())
end)


@Forfex

thank you i will try to do

I’ll try to adapt here, anything I say, thank you very much for your help.

Yep, my bad you should use event.key instead.

help fivem script to hold down a button and keep it pressed continuously so that the menu remains open, after leaving it pressed the menu disappears

if IsControlJustPressed(0,44) then
ToggleActionMenu()
end

function ToggleActionMenu()
openPauseMenu()
else
closePauseMenu()
end
end