button.MouseEnter:Connect(function()
spawn(function()
while true do
for i = 1, 360 do
camera.CFrame = clone.PrimaryPart.CFrame*CFrame.Angles(0, math.rad(i), 0)*offset
wait()
end
button.MouseLeave:Connect(function()
camera.CFrame = clone.PrimaryPart.CFrame*offset
break
end)
end
end)
end)
Obviously this won’t work. But how can I make it work? The idea is when the mouse enters a button it starts rotating, once it leaves it would stop rotating
This is unrelated, but can’t you just use TweenService to move the camera? It would be so much easier.
Edit:
local TweenService = game:GetService("TweenService")
local TweenSettings = TweenInfo.new(10,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,math.huge,false,0)
local CameraTween = TweenService:Create(Camera,TweenSettings,{CFrame = clone.PrimaryPart.CFrame*CFrame.Angles(0, math.rad(360), 0)*offset})
button.MouseEnter:Connect(function()
CameraTween:Play()
end
button.MouseLeave:Connect(function()
CameraTween:Stop()
end
As for what the OP is asking, I think the while statements should be setup like this
button.MouseEnter:Connect()
local IsMouseEntered = true
while IsMouseEntered do
--code to move camera
button.MouseLeave:Connect(function()
IsMouseEntered = false
end)
end
end
You could use a boolean which will be set to false when the MouseLeave event is fired.
button.MouseEnter:Connect(function()
local mouseEntered = true
spawn(function()
while mouseEntered do
for i = 1, 360 do
if (not mouseEntered) then
break
end
camera.CFrame = clone.PrimaryPart.CFrame*CFrame.Angles(0, math.rad(i), 0)*offset
wait()
end
end
camera.CFrame = clone.PrimaryPart.CFrame*offset
end)
local connection
connection = button.MouseLeave:Connect(function()
mouseEntered = false
connection:Disconnect()
end)
end)
In my opinion the best way to do it is with a boolean variable.
local deb = false
button.MouseEnter:connect(function()
deb = true
spawn(function()
while deb do
for i = 1, 360 do
if deb then
camera.CFrame = clone.PrimaryPart.CFrame*CFrame.Angles(0, math.rad(i), 0)*offset
wait()
else
break
end
end
end
end)
button.MouseLeave:connect(function()
deb = false
end)
I think that the code above will work, if not, something close to it will do the work.
The easiest way to do this is to use a return. Returning while in a function will stop execution of a function (can also be used for returning values, but not relevant)
button.MouseEnter:Connect(function()
spawn(function()
--Also, why are you using spawn? You do not have to, as connect wraps into it's own thread
while true do
for i = 1, 360 do
camera.CFrame = clone.PrimaryPart.CFrame*CFrame.Angles(0, math.rad(i), 0)*offset
wait()
end
button.MouseLeave:Connect(function()
camera.CFrame = clone.PrimaryPart.CFrame*offset
return --Stop function
end)
end
end)
end)
You could also use a variable, but that requires more lines of code.
The reason I went with BindToRenderStep over TweenService in this case is because with BindToRenderStep, you’re able to unbind from any other script on the client, which you may end up wanting (or needing) to do.
Chances are that both events, button.MouseEnter and button.MouseLeave, could be firing multiple times at the same time, causing the bind and unbind functions to be called multiple times as well.
This is most likely because you entering and leaving a button at the same time, such as between buttons in your GUI. This could even happen if you place the buttons some pixels apart, since your cursor can travel multiple pixels every frame.
The simplest way to solve this is to unbind whatever function is binded to ‘CameraRotation’ at the time the event button.MouseEnter is being fired before binding a different function to it. I am pretty sure there is no harm from unbinding a function multiple times as a sanity check.
A different solution to this would simply be to make sure to bind and unbind a function once using a boolean to check whether the function has been binded or not.
Using this, plus your use of a base instead of an offset: