RightMouseButton not working

I am trying to make it so when I click the right mouse button with a tool equiped it will make a gui frame invisible and when the right mouse button isn’t clicked anymore it comes back, it just doesn’t seem to work

local gui = game.Players.LocalPlayer.PlayerGui.Crosshair:FindFirstChild("Holder")

local UserInputService = game:GetService("UserInputService")
local isRightMouseButtonDown = false

UserInputService.InputBegan:Connect(function(input, isProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isRightMouseButtonDown = true
		if gui then
			gui.Visible = false
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, isProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isRightMouseButtonDown = false
		if gui then
			gui.Visible = true
		end
	end
end)
6 Likes

Where’s the local script located?

2 Likes

under the tool
321321312132312321231

what, idk what u just said but the frame goes invisible when you hold right click and then goes visible when you stop right click

Actually you are ryt, I just realized how brain dead that reply was mb

Anyways its prolly due to this line

local gui = game.Players.LocalPlayer.PlayerGui.Crosshair:FindFirstChild("Holder")

If the gui wasn’t loaded beforehand this would default to nil, which won’t trigger this if statement

Try printing the gui value to see if this is the case

1 Like

ive figured it out, its just now i wanna make it so when you click the right mouse button you dont have to hold it down, you can just click it and then let go

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, isProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		gui.Visible = not gui.Visible
	end
end)

alright so that works issue is now the script will only work once, so how would i create a loop to check for when the button is being pressed so it makes the gui invis and vis

Huh? That’s wrong cuz it’s an event which fires every time the RMB is clicked and inverses the visible property of the gui

well when you unequip the gun, the script no longer works

Do you mean like this?

UserInputService.InputBegan:Connect(function(input, isProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if gui.Visible then
		  gui.Visible = false
		else
		   gui.Visible = true
		end
	end
end)

no the script works on the first equip but once i unequip it and equip it for a 2nd time it doesnt work

I replied to the message where you said this, but try the one I sent anyway:

the one he sent worked but i tried yours and it does work but what about the new issue?

Try

local gui = game.Players.LocalPlayer.PlayerGui.Crosshair:FindFirstChild("Holder")
if gui.Visible then
	gui.Visible = false
else
	gui.Visible = true
end

Add some prints around, im not rly sure what’s happening here, (unless you added in more code)

also @KrulTsepeshi you are essentially doing the same thing as me but more code so ye just wanted to lyk

local gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("Crosshair").Holder

local UserInputService = game:GetService("UserInputService")
local isRightMouseButtonClicked = false



UserInputService.InputBegan:Connect(function(input, isProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if gui.Visible then
			gui.Visible = false
		else
			gui.Visible = true
		end
	end
end)

i added the prints to it and it dectets the holder script but for some reason it doesn’t make it go invisible after the 2nd equip

local gui = game.Players.LocalPlayer.PlayerGui.Crosshair:FindFirstChild("Holder")

local UserInputService = game:GetService("UserInputService")

local connections = table.create(2)

tool.Equipped:Connect(function()
	connections[1] = UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
		
		gui.Visible = false
	end)
	
	connections[2] = UserInputService.InputEnded:Connect(function(input)
		if input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end

		gui.Visible = true
	end)
end)

tool.Unequipped:Connect(function()
	for _, connection in connections do
		connection:Disconnect()
	end
end)
1 Like