I'm trying to make a gui shake a little when I hover my mouse over it

I’m trying to make it so when you hover your mouse over a textbutton it shakes a little. Here is the script I tried using, but didn’t work how I thought it would. I want it so it stops as soon as the mouse leaves the button. So far it rotates to the right without stopping.

function Entered()
    while true do
        wait(0.1)
        script.Parent.Rotation = script.Parent.Rotation + 1
    end
end

function Leave()
    script.Parent.Rotation = 0
end


script.Parent.MouseEnter:connect(Entered)
script.Parent.MouseLeave:connect(Leave)

Please let me know what I did wrong, thanks.

There is nothing stopping the while loop from ending, thus is continues infinitely. To fix this I recommend just adding a debounce that breaks the loop. It would look like this:

local debounce = false

function Entered()
    debounce = true

    while wait(0.1) do

        if debounce then
            break
        end

        script.Parent.Rotation = script.Parent.Rotation + 1
    end
end

function Leave()
    debounce = false
    script.Parent.Rotation = 0
end


script.Parent.MouseEnter:connect(Entered)
script.Parent.MouseLeave:connect(Leave)

Is that supposed to be in a local script? I tried using that script in a normal script and in result it wouldn’t rotate.

Yes, it needs to be in a local script. This is because a server-sided script cannot directly change properties that are client-sided, such as GUIs.

Weird, for some reason it just stops rotating, any idea why? Even after I put it in a local script.

Sorry, the if statement should be

if not debounce then

instead of

if debounce then
3 Likes