Script still recognizes Mousebutton1 click even tho it shouldnt

I have this script and when I put the mouse over the part called Alt and left click it,it prints innit bruv like it should the problem is when I move my mouse outside the part it still recognizes the left click,how can I fix/disable this?

local Mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local character = player.Character
local dis = false
while true do wait()
	local ray = Ray.new(
		Mouse.Origin.p,
		Mouse.UnitRay.Direction * 50
	)

	local hit = workspace:FindPartOnRay(ray, character)
	local name = hit ~= nil and hit.Name
	if name == "Alt" then
		Mouse.Button1Down:connect(function()
			if dis == false then
				dis = true
				print("innit bruv")
				wait(0) 
				dis = false 
			end
		end)
	end	
end	

It is because there is a while true do loop, it will continue printing unless there is something to stop it from printing. (like a boolean)

A much cleaner way to do this would be

local player = game.Players.LocalPlayer
local character = player.Character
local Mouse = player:GetMouse()
local dis = false

Mouse.Button1Down:connect(function()
	local ray = Ray.new(
		Mouse.Origin.p,
		Mouse.UnitRay.Direction * 50
	)

	local hit = workspace:FindPartOnRay(ray, character)
	local name = hit ~= nil and hit.Name
	if name == "Alt" and not dis then
		dis = true
		print("innit bruv")
		wait() 
		dis = false 
	end
end)

This way, when you press left click, it’ll make a ray of 50 studs to see if the mouse is touching a part called Alt, and if it is and the debounce is false, print Innit bruv. It’s much more better than constantly using a while loop which is not really optimal i nthis case

The problem with this script is I have this in a plane tool and the script before worked after it got at a certain speed this script doesnt

Could you reexplain what you mean by that? I’m slightly not understanding

So I have a plane right and this script is for the buttons in the cockpit,if the plane is static your script works like the one I had before now after the plane takes off your script no longer works while the one I had before does but the script I had before has the problem I mentioned before

I think what you could do instead is use Clickdetectors for that case rather than this, I think.

nope they also dont work as explained here Click Detector on fast moving part cannot be clicked and mouse icon flickers - #8 by IISato

I think what you could do instead to check if the player is close enough to use the cockpit is instead using

local player = game.Players.LocalPlayer
local character = player.Character
local Mouse = player:GetMouse()
local dis = false

Mouse.Button1Down:connect(function()
	local dis = (Mouse.Target.p - character.HumanoidRootPart.Position).magnitude
	local hit = Mouse.Target
	local name = hit ~= nil and hit.Name
	if dis <= 50 and name == "Alt" and not dis then
		dis = true
		print("innit bruv")
		wait() 
		dis = false 
	end
end)

This subtracts where the mouse is pointing and the position of your humanoidrootpart and returns the magnitude (units away) you both are, and if it’s less than or equal to 50 and the name is Alt and dis is false, print. I believe this is how it’s done

I am using mesh parts so its says .p is not a valid member

Above local dis =, could you put a print(Mouse.Target) and try again

Edit: Try replacing Mouse.Target.p to Mouse.Hit.p

it prints baseplate once and thats it

and also no matter what part I click it always says .p is not a valid member

Then I think it should’ve been Mouse.Hit.p since I now remember .p is to get the position of a CFrame, which Mouse.Hit is

switched it but nothing happens

Any errors? After local dis try doing print(dis) and seeing what it gives you

local Mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local character = player.Character
local dis = false
while true do wait()
    local ray = Ray.new(
        Mouse.Origin.p,
        Mouse.UnitRay.Direction * 50
    )

    local hit = workspace:FindPartOnRay(ray, character)
    if hit ~= nil then
        local name = hit.Name
        if name == "Alt" then
        local Connection =    Mouse.Button1Down:Connect(function()
           Mouse.Button1Up:Connect(function()
            Connection:Disconnect()  
           end)
                if dis == false then
                    dis = true
                    print("innit bruv")
                    wait(0) 
                    dis = false 
                end
            end)
        end    
    end
end

it prints numbers but clicking on the alt part nothing happens

1 Like

error with attempt to index nil with “Disconnect” at line 17

Make a connection variable somewhere before the while loop and remove the local from the connection i nthe loop

local Mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local character = player.Character
local dis = false
local Connection
while true do wait()
    local ray = Ray.new(
        Mouse.Origin.p,
        Mouse.UnitRay.Direction * 50
    )

    local hit = workspace:FindPartOnRay(ray, character)
    if hit ~= nil then
        local name = hit.Name
        if name == "Alt" then
			Connection = Mouse.Button1Down:Connect(function()
                if dis == false then
                    dis = true
                    print("innit bruv")
                    wait(0) 
                    dis = false 
                end
            end)
			Mouse.Button1Up:Connect(function()
				Connection:Disconnect()  
			end)
        end    
    end
end