Need help stopping a function after mouse leave

Hello,

I need help trying to figure out how to stop the part from rotating in a viewport frame after the mouse leaves the viewport frame.

This is my current code:

I tried using an if statement but it didn’t work.

Thanks,
Skylexion

ViewportFrame.MouseEnter:Connect(function()
    local MouseLeft = false
    ViewportFrame.MouseLeave:Connect(function()
        MouseLeft = true
    end)
    for Degrees = 0, 360, Increase do
        Part.Orientation = Vector3.new(0, Degrees, 0)
        wait()
        if MouseLeft then
            Part.Orientation = Vector3.new(0, 0, 0)
            break
        end
    end
end)
1 Like