More consistent button transition

I’m working on the main menu for my game. Each text button has a local script where it does a simple tween when the mouse hovers over it, but the mouse enter/mouse leave events don’t fire the functions consistently.

I’ve tried other events such as Input Began/Ended, but it doesn’t seem to change. What else should I try instead?

local buttonTable = {playButton,settingsButton,creditsButton,backButton}

while inMainMenu == true do
	for _, Button in pairs(buttonTable) do
		local object = Button
		local objPos = Button.Position
		
        -- shifts the button more to the right using scale instead of pixels 
		function shiftPos()
			object:TweenPosition(UDim2.new(0.2,0,objPos.Y.Scale,0))
		end
        -- returns the button to the original position
		function returnPos()
			object:TweenPosition(UDim2.new(0.1,0,objPos.Y.Scale,0))
		end
	   -- mouse enter/leave events still doesn't fire events perfectly
		object.MouseEnter:Connect(shiftPos)
		object.MouseLeave:Connect(returnPos)
	end
	wait(0.2)
end

1 Like

I don’t know specifically how to fix your problem, but you should just loop through all the buttons in a single script instead of putting a script under every button!! It’s bad to have 50 different scripts because you won’t be able to edit them easily and it’s unorganized

2 Likes

You may just make a LocalScript that loops through all said buttons in a specific folder with this.

local folder = game.Players.LocalPlayer.PlayerGui.ScreenGui.ButtonFolder:GetChildren()

while debounce == true do

  for _, Button in pairs(folder) do
	
	function shiftPos(Input)

		object:TweenPosition(objPos + UDim2.new(0,50,0,0))
		
 	end


	function returnPos()
		
		object:TweenPosition(objPos)
		
	end

	object.MouseEnter:Connect(shiftPos)
	object.MouseLeave:Connect(returnPos)

 end

 wait(0.2)
      
end

the debounce value will be to check if the player has exited the Menu.

2 Likes

why did you put this in a infinite while loop? this is a massive memory leak it’s going to make 20 connections per second constantly while you play lol

My bad, I meant to change that to a debounce value to check if they are still in the main menu.

Thank you for the feedback! I’ve put all the animations in one script only now, and it seems like the loop made the buttons more responsive.

However, the event can still be inconsistent, so I’ll probably rename this topic to suit that problem.

Changes will be seen on the original post