Help Disabling "Right Mouse Button" And "X" When layer Touches Part

Hello! So basically what I am doing is creating a third person shooter system however, I need help with one thing.

What I would like to achieve is to disable “Right mouse button” and “X” whenever the player touches a part (which will be put in water and set to transparent)
I have tried multiple scripts and getting pretty desperate, please help me out!

1 Like

So you’re saying something like:

Player touches part → Disable right mouse button and X key

am I correct? If you’re using UserInputService, you can disconnect the connection upon touching a part.

1 Like

Yeah, but I want it so that when the player stops touching the part, the Right Mouse Button and X key can still be able to work.

I think TouchEnded is what you’re looking for? Just rebind right mouse and the X key once it fires.

If you use ContextActionService you are easily able to enable (bind) and disable (unbind) keys when you need to.

So your code would look something like this:

local ContextActionService = game:GetService("ContextActionService")

local function enableInput()
	ContextActionService:BindAction("RightClick", Function, false,  Enum.UserInputType.MouseButton2) -- Right Click | Replace "Function" with the function that the action runs |
	ContextActionService:BindAction("X", Function, false, Enum.KeyCode.X ) -- X | Replace "Function" with the function that the action runs |
end

local function disableInput()
	ContextActionService:Unbind("RightClick")
	ContextActionService:Unbind("X")
end