Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make a gui button rotate back and forth to make it not static?

I am new to Roblox Lua and probably for most people, I have come to roadblocks. My friend and I are making a new game and I want the GUI to not feel static but lively. I designed the GUI frames and made an image button but I have written my own code to do it but keep on coming up with that the loops can overlap of you go back and forth very fast. I used "mouseEnter" and "mouseLeave" to detect it but I can't figure out how to stop the loop right when I leave it. I have spent many hours trying to figure it out but can't.

Here is my code:

local button = script.Parent

	
button.MouseEnter:Connect(function()
	while true do
		button.Rotation = 0
			wait (.03)
		button.Rotation = -1
			wait (.03)
		button.Rotation = -2
			wait (.03)
		button.Rotation = -3
			wait (.03)
		button.Rotation = -4
			wait (.03)
		button.Rotation = -5
			wait (.03)
		button.Rotation = -6
			wait (.03)
		button.Rotation = -7
			wait (.03)
		button.Rotation = -8
			wait (.03)
		button.Rotation = -9
			wait (.03)
		button.Rotation = -8
			wait (.03)
		button.Rotation = -7
			wait (.03)
		button.Rotation = -6
			wait (.03)
		button.Rotation = -5
			wait (.03)
		button.Rotation = -4
			wait (.03)
		button.Rotation = -3
			wait (.03)
		button.Rotation = -2
			wait (.03)
		button.Rotation = -1
			wait (.03)
		button.Rotation = 0
			wait (.03)
		button.Rotation = 1
			wait (.03)
		button.Rotation = 2
			wait (.03)
		button.Rotation = 3
			wait (.03)
		button.Rotation = 4
			wait (.03)
		button.Rotation = 5
			wait (.03)
		button.Rotation = 6
			wait (.03)
		button.Rotation = 7
			wait (.03)
		button.Rotation = 8
			wait (.03)
		button.Rotation = 9
			wait (.03)
		button.Rotation = 8
		wait (.03)
		button.Rotation = 7
			wait (.03)
		button.Rotation = 6
			wait (.03)
		button.Rotation = 5
			wait (.03)
		button.Rotation = 4
			wait (.03)
		button.Rotation = 3
			wait (.03)
		button.Rotation = 2
			wait (.03)
		button.Rotation = 1
			wait (.03)
			print ("hi")
		end
end)

button.MouseLeave:Connect(function()
button.Rotation = 0
end)

The code can probably be shortened but I just need a way to figure out how to get the loop to stop right when the mouse leaves the GUI button.

SOLVED------- I took the code the given and worked with it for a bit, coming up with the following:

local button = script.Parent
local IsHovering = false
button.MouseEnter:Connect(function()
    IsHovering = true
end)
button.MouseLeave:Connect(function()
    IsHovering = false
end)
spawn(function()
    while true do wait()
        if IsHovering == true then
            for count = 1,9,1 do wait(.03)
                button.Rotation = count*-1
            end
			for count = 9,1,-1 do wait(.03)
                button.Rotation = count*-1
            end
  			for i = 1,9,1 do wait(.03)
                button.Rotation = i
            end
			for i = 9,1,-1 do wait(.03)
                button.Rotation = i
            end
        else
            button.Rotation = 0
        end
    end
end)

Thanks for the code.

2 answers

By the way this code I put in my main post works for the Mouseenter part, but doesn't work for the mouseleave part. It worked earlier but I still had the same issue.

Try this:

local button = script.Parent
local IsHovering = false
button.MouseEnter:Connect(function()
	IsHovering = true
end)
button.MouseLeave:Connect(function()
	IsHovering = false
end)
spawn(function()
	while true do wait()
		if IsHovering == true then
			for i = 1,9,1 do wait(.03)
				button.Rotation = i*-1
			end
			for i = 1,9,1 do wait(.03)
				button.Rotation = i
			end
		else
			button.Rotation = 0
		end
	end
end)

Basically I made a variable, when its false the buttons rotation is set to 0 otherwise its rotation is looped. if this works, accept the answer.

4 comments

0

First off, thank you. There is one issue though. When I tested this the rotations were a not as smooth as I wanted. If there is a way to tweak that can you tell me? Thanks a lot though.

0

Yeah you can tween them using Tween service

0

How exactly would I do that?

This discussion is preserved as it was. New answers, comments, and votes are closed.