Viewport Spinning when Mouse Hovers

hi!!! i am a sort of beginner with scripting. i know most of the basics and need help on something.

i am trying to make a viewport frame that shows a part. i want that part to spin when the players mouse is hovering over the gui. and also when the mouse leaves that gui then the part stays in the same position it is in. if anyone can help w this i would rlly appreciate!! thank uu <33

heres the script i am using. i do not know how to make it freeze when mouse leaves.

local frame = script.Parent.Parent

local part = script.Parent

local partSpeed = 0.01

frame.MouseEnter:Connect(function()

while wait() do

part.CFrame = part.CFrame * CFrame.Angles(0,partSpeed,0)

end

end)

and here is my workspace:
image

1 Like

The simplest way would be to keep a variable around like local spinning = false at the top of your script.

Then move your while loop to the bottom of your script, outside the event.

Wrap the CFrame stuff in an if spinning then.

Then making the part start/stop is just a matter of setting spinning = true or false. That’s all you need to do in your MouseEnter/MouseLeave events.

thank u for the help!! im a bit confused. is this what you mean?

local spinning = false

local frame = script.Parent.Parent

local part = script.Parent

local partSpeed = 0.01

frame.MouseEnter:Connect(function()

if spinning then

part.CFrame = part.CFrame * CFrame.Angles(0,partSpeed,0)

end

end)

frame.MouseLeave:Connect(function()

local spinning = false

end)

You can format it as code by typing three ` in a row and writing your code after that line.

1 Like

‘’’
local spinning = false

local frame = script.Parent.Parent

local part = script.Parent

local partSpeed = 0.01

frame.MouseEnter:Connect(function()

if spinning then

part.CFrame = part.CFrame * CFrame.Angles(0,partSpeed,0)

end

end)

frame.MouseLeave:Connect(function()

local spinning = false

end)

Sorry it is three of these: `

Just copy and paste the character three times in front of your code.

1 Like
local spinning = false

local frame = script.Parent.Parent

local part = script.Parent

local partSpeed = 0.01

frame.MouseEnter:Connect(function()

if spinning then

part.CFrame = part.CFrame * CFrame.Angles(0,partSpeed,0)

end

end)

frame.MouseLeave:Connect(function()

local spinning = false

end)
2 Likes

Close, but the “if spinning” block should be inside its own while loop at the bottom of your code, outside of any events. The MouseEnter event itself should just be spinning = true

Also remove the local from MouseLeave. You want to reassign the existing variable, not create a new one.

u mean something like this?

local spinning = false

local frame = script.Parent.Parent

local part = script.Parent

local partSpeed = 0.01

frame.MouseEnter:Connect(function()

spinning = true

end)

frame.MouseLeave:Connect(function()

spinning = false

end)

while true do

if spinning then

part.CFrame = part.CFrame * CFrame.Angles(0,partSpeed,0)

end

end

Yup! That should do it. Does it make sense how it works?

1 Like