[Release] ActionMenu [1.0.1] (Updated April 2018)

and I have made no modifications to the actionmenu

What kind of keyboard are you using? Also, try resetting your keybinds, that sometimes does the trick.

I am gonna try changing the key to open the menu. Will report back to you :stuck_out_tongue:

EDIT: Changed key to F6 and it works great. Thanks @WolfKnight179 !

1 Like

Does anyone know why my sub menus are not working? I read the page, but it was kinda confusing to me. I have tried a variety of things and the menus itself works but the sub menus dont. Here is my code for the menu:

<div id="mainmenu">
            <button class="menuoption" data-action="Vehicle">Vehicle</button>
            <button class="menuoption" data-action="LEO">LEO</button>
            <button class="menuoption" data-action="Civilian">Civilian</button>
		</div>
		
		<div id="vehicle" data-parent="mainmenu" style="display: none;">
			<button class="menuoption" data-sub="Engine">Turns Engine On/Off</button>
			<button class="menuoption" data-sub="Trunk">Opens Trunk</button>
			<button class="menuoption" data-sub="Windows">Rolls Down Window</button>
			<button class="menuoption" data-sub="Doors">Opens Doors</button>
		</div>
			
		<div id="LEO" data-parent="mainmenu" style="display: none;">
			<button class="menuoption" data-sub="Stun Gun">Equips Stun Gun</button>
			<button class="menuoption" data-sub="Cuff">Cuffs Player</button>
			<button class="menuoption" data-sub="Drag Player">Drags Player</button>
			<button class="menuoption" data-sub="Put In Vehicle">Puts Player In Vehicle</button>
			<button class="menuoption" data-sub="Take Player Out Of Vehicle">Takes Player Out Of Vehicle</button>
		</div>
		
		<div id="Civilian" data-parent="mainmenu" style="display: none;">
			<button class="menuoption" data-sub="Assault Rifle">Equips Assault Rifle</button>
			<button class="menuoption" data-sub="Pistol">Equips Pistol</button>
			<button class="menuoption" data-sub="Handsup">Puts Hands Up</button>
			<button class="menuoption" data-sub="Handsup Knees">Gets On Knees With Hands Up</button>

You have a few missing <div> tags in your code. Make sure you are opening and close each <div> and it should work fine.

You’re code needs to look like this. You had “data-sub” where “data-action” should of been.

        <div id="mainmenu">
            <button class="menuoption" data-sub="vehicle">Vehicle</button>
            <button class="menuoption" data-sub="leo">LEO</button>
            <button class="menuoption" data-sub="civ">Civilian</button>
		</div>
		
		<div id="vehicle" data-parent="mainmenu" style="display: none;">
			<button class="menuoption" data-action="engine">Turns Engine On/Off</button>
			<button class="menuoption" data-action="trunk">Opens Trunk</button>
			<button class="menuoption" data-action="windows">Rolls Down Window</button>
			<button class="menuoption" data-action="doors">Opens Doors</button>
		</div>
			
		<div id="leo" data-parent="mainmenu" style="display: none;">
			<button class="menuoption" data-action="stun_gun">Equips Stun Gun</button>
			<button class="menuoption" data-action="cuff">Cuffs Player</button>
			<button class="menuoption" data-action="drag">Drags Player</button>
			<button class="menuoption" data-action="seat">Puts Player In Vehicle</button>
			<button class="menuoption" data-action="unseat">Takes Player Out Of Vehicle</button>
		</div>
		
		<div id="civ" data-parent="mainmenu" style="display: none;">
			<button class="menuoption" data-action="assault_rifle">Equips Assault Rifle</button>
			<button class="menuoption" data-action="pistol">Equips Pistol</button>
			<button class="menuoption" data-action="handsup">Puts Hands Up</button>
			<button class="menuoption" data-action="handsup_knees">Gets On Knees With Hands Up</button>
        </div>
			<!-- Do not remove this or you will not be able to exit the menu --> 
			<button class="menuoption" data-action="exit">Exit</button>
        </div>
2 Likes

Anyone no how to properly trigger a .lua script to one of these buttons?

Thanks man it worked!

Now I am like you, just need to get the lua to trigger

How can I make this open only for as long as I hold the control down? I want to be able to quickly enter and exit the menu without having to click exit?

You have to implement the menu to your .lua script or vice-versa or just call specific events in other scripts instead of the chatPrint.

Say you had a function for handsUp and a button for it. When you click the button for Hands Up, it sends the button data to the callback buttonclick. If the hands up button was set to button1, where it says " chatPrint( “Button 1 pressed!” ) " You would put in there, what happens when you press button1.

So instead of " chatPrint( “Button 1 pressed!” ) " you could have " handsUp() "

RegisterNUICallback( "ButtonClick", function( data, cb ) 
	if ( data == "button1" ) then 
		handsUp()
	elseif ( data == "button2" ) then 
		chatPrint( "Button 2 pressed!" )
	elseif ( data == "button3" ) then 
		chatPrint( "Button 3 pressed!" )
	elseif ( data == "button4" ) then 
		chatPrint( "Button 4 pressed!" )
	elseif ( data == "exit" ) then 
		-- We toggle the ActionMenu and return here, otherwise the function 
		-- call below would be executed too, which would just open the menu again 
		ToggleActionMenu()
		return 
	end

Which would go into the example function "handsUp() " like I have below and do all this junk.

local handsup = false

function handsUp()
    local dict = "missminuteman_1ig_2"
    
	RequestAnimDict(dict)
	while not HasAnimDictLoaded(dict) do
		Citizen.Wait(0)
	end
	if not handsup then
		TaskPlayAnim(GetPlayerPed(-1), dict, "handsup_enter", 8.0, 8.0, -1, 50, 0, false, false, false)
		handsup = true
	else
		handsup = false
		ClearPedTasks(GetPlayerPed(-1))
	end
end

You just replace the chatPrint with where ever you want the code to go. It can go anywhere… You could even slap your whole function right in there, only that’s not very tidy and not a good idea. Better to send the code off to do something.

3 Likes

Thanks for explaining @Decon

Hey, no problem. We’re all here working together right? :wink:

Have you renamed the resource?

i did rename the resource

This does not seem to be working, probably did it wrong lol

--[[------------------------------------------------------------------------

	ActionMenu 
	Created by WolfKnight
	Additional help from lowheartrate, TheStonedTurtle, and Briglair. 

------------------------------------------------------------------------]]--

-- Define the variable used to open/close the menu 
local menuEnabled = false 

local handsup = false
--[[------------------------------------------------------------------------
	ActionMenu Toggle
	Calling this function will open or close the ActionMenu. 
------------------------------------------------------------------------]]--
function ToggleActionMenu()
	-- Make the menuEnabled variable not itself 
	-- e.g. not true = false, not false = true 
	menuEnabled = not menuEnabled

	if ( menuEnabled ) then 
		-- Focuses on the NUI, the second parameter toggles the 
		-- onscreen mouse cursor. 
		SetNuiFocus( true, true )

		-- Sends a message to the JavaScript side, telling it to 
		-- open the menu. 
		SendNUIMessage({
			showmenu = true 
		})
	else 
		-- Bring the focus back to the game
		SetNuiFocus( false )

		-- Sends a message to the JavaScript side, telling it to
		-- close the menu.
		SendNUIMessage({
			hidemenu = true 
		})
	end 
end 

--[[------------------------------------------------------------------------
	ActionMenu HTML Callbacks
	This will be called every single time the JavaScript side uses the
	sendData function. The name of the data-action is passed as the parameter
	variable data. 
------------------------------------------------------------------------]]--
RegisterNUICallback( "ButtonClick", function( data, cb ) 
	if ( data == "button1" ) then 
		handsUp()
	elseif ( data == "button2" ) then
		chatPrint( "Button 2 pressed!" )
	elseif ( data == "button3" ) then 
		chatPrint( "Button 3 pressed!" )
	elseif ( data == "button4" ) then 
		chatPrint( "Button 4 pressed!" )
	elseif ( data == "exit" ) then 
		-- We toggle the ActionMenu and return here, otherwise the function 
		-- call below would be executed too, which would just open the menu again 
		ToggleActionMenu()
		return 
	end 

	-- This will only be called if any button other than the exit button is pressed
	ToggleActionMenu()
end )


--[[------------------------------------------------------------------------
	ActionMenu Control and Input Blocking 
	This is the main while loop that opens the ActionMenu on keypress. It 
	uses the input blocking found in the ES Banking resource, credits to 
	the authors.
------------------------------------------------------------------------]]--
Citizen.CreateThread( function()
	-- This is just in case the resources restarted whilst the NUI is focused. 
	SetNuiFocus( false )

	while true do 
		-- Control ID 20 is the 'Z' key by default 
		-- Use https://wiki.fivem.net/wiki/Controls to find a different key 
		if ( IsControlJustPressed( 1, 167 ) ) then 
			ToggleActionMenu()
		end 

	    if ( menuEnabled ) then
            local ped = GetPlayerPed( -1 )	

            DisableControlAction( 0, 1, true ) -- LookLeftRight
            DisableControlAction( 0, 2, true ) -- LookUpDown
            DisableControlAction( 0, 24, true ) -- Attack
            DisablePlayerFiring( ped, true ) -- Disable weapon firing
            DisableControlAction( 0, 142, true ) -- MeleeAttackAlternate
            DisableControlAction( 0, 106, true ) -- VehicleMouseControlOverride
        end

		Citizen.Wait( 0 )
	end 
end )

function chatPrint( msg )
	TriggerEvent( 'chatMessage', "Dev", { 255, 255, 255 }, msg )
end 

function handsUp()
    local dict = "missminuteman_1ig_2"
    
	RequestAnimDict(dict)
	while not HasAnimDictLoaded(dict) do
		Citizen.Wait(0)
	end
	if not handsup then
		TaskPlayAnim(GetPlayerPed(-1), dict, "handsup_enter", 8.0, 8.0, -1, 50, 0, false, false, false)
		handsup = true
	else
		handsup = false
		ClearPedTasks(GetPlayerPed(-1))
	end
end

Open ui.js, go to line 78 and change “wk_actionmenu” to your resource name.

Not sure what you did wrong there but it seems to be working just fine for me. I’m running the exact same code as you, the only difference is that I can’t see your HTML and if your buttons are set up correctly or not.
If you left them default, it should be working. You could try putting the chatPrint back in there to assure that the button is actually being fired. My guess is that your HTML has been modified and not getting registered?

RegisterNUICallback( "ButtonClick", function( data, cb ) 
	if ( data == "button1" ) then 
		chatPrint( "Button 1 pressed!" )
		handsUp()
		drawNotification("Button 1 just fired!")
	elseif ( data == "button2" ) then
		chatPrint( "Button 2 pressed!" )
	elseif ( data == "button3" ) then 
		chatPrint( "Button 3 pressed!" )
	elseif ( data == "button4" ) then 
		chatPrint( "Button 4 pressed!" )
	elseif ( data == "exit" ) then 
		-- We toggle the ActionMenu and return here, otherwise the function 
		-- call below would be executed too, which would just open the menu again 
		ToggleActionMenu()
		return 
	end

I personally use a little function for Admin Notifications simply because I don’t like using the chat window. The chat window just gets in my way. The function I use is below. I put a line up in the button 1 pressed if you wanted to use this instead of the chatPrint so you can see how it would get used.

function drawNotification(text)
	SetNotificationTextEntry("STRING")
	AddTextComponentString(text)
	DrawNotification(false, false)
end

Also, since this was just an example function, you actually only have to load that animation once, it doesn’t need to be loaded every time the button is fired. But I guess it doesn’t hurt loading it every time. Just redundant.
You could also add some console prints to debug your function, to see what’s happening when you’re ingame. Just slap in some:
print("Is this working?")
or whatever you are trying to fix. Just press F8 ingame to see if your prints show up in the console.

Did you rename your resource?

No I did not, will test when I’m home

Thanks a lot :smiley: