Jump to content

script to avoid using CTRL and left mouse button


theus.py

Recommended Posts

1 hour ago, Hydra said:

Shouldn't it be?
 

if key == "lctrl" and key == "mouse1" then
   cancelEvent()
end

 

No.

Since "lctrl" and "mouse1" are not equal to each other, If key is "lctrl" then by definition it cannot be "mouse1", therefore this conditional always evaluates false, and cancelEvent is never called.

If you want to block either lctrl or mouse1, keep the logical connective as or.

addEventHandler("onClientKey", root,
  function(key, pressState)
    if key == "lctrl" or key == "mouse1" then -- block the use of either key
      cancelEvent(true)
    end
  end
)

If you want to block both from being used at the same time, you instead have to use an internal state to remember whether the other key is pressed. For example, have a variable (bool) remember whether lctrl is currently pressed or not, and when key == "mouse1" you can test

local lctrlIsPressed = -- assigned at previous invocation of onClientKey

addEventHandler("onClientKey", root,
  function(key, pressState)
    if key == "lctrl" then
      lctrlIsPressed = pressState -- remember the pressState of lctrl
    elseif key == "mouse1" and pressState and lctrlIsPressed == true then -- only if mouse1 was now pressed and lctrl is already pressed
      cancelEvent(true)
    end
  end
)

Note the example above is only half correct; it prevents the detection of pressing mouse1 while lctrl is pressed, but not the opposite; if one clicks and holds mouse1 and only then presses lctrl, this will not be prevented from detection by the game.

Edited by Addlibs
  • Like 1
Link to comment
On 30/09/2023 at 20:46, theus.py said:

Can someone help me?
I'm trying to create a script for the MTA that prevents the player from using CTRL and using the left mouse button, what can I do???

NOTE: This is while the player is moving, if he is moving 'run' he will not be able to use CTRL and the left mouse button

Just now, theus.py said:

NOTE: This is while the player is moving, if he is moving 'run' he will not be able to use CTRL and the left mouse button

 

On 30/09/2023 at 20:46, theus.py said:

 

 

Link to comment
2 hours ago, theus.py said:

NOTE: This is while the player is moving, if he is moving 'run' he will not be able to use CTRL and the left mouse button

 

 


function checkisPlayerRunning(_, state)
   if state == "down" then 
      addEventHandler("onClientKey", root, cancelControls)
   else 
      removeEventHandler("onClientKey", root, cancelControls)
   end 

end 
bindKey("sprint", "up", checkisPlayerRunning)
bindKey("sprint", "down", checkisPlayerRunning)

function cancelControls(key, pressState)
    if key == "lctrl" or key == "mouse1" then -- block the use of either key
      cancelEvent(true)
    end
end 

try this, maybe it will help I'm not too sure

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...