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
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
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 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
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
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