Hover animation not working the first time, but works every other time

I made a hover animation for a button in my game, but it doesn’t work the first time I hover over the button. But every other time after that, it works. Does anyone know how to fix this?

Script:

local Button = script.Parent
local woosh = script.Woosh

--HOVER ANIMATION
Button.MouseEnter:Connect(function()
	local player = game.Players.LocalPlayer
	local TweenService = game:GetService("TweenService")

	Button.MouseEnter:Connect(function()
		local Info = TweenInfo.new(
			0.2,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.InOut,
			0,
			false,
			0
		)

		local Tween = TweenService:Create(Button, Info, {Rotation = 15})
		Tween:Play()
		woosh:Play()
	end)

	Button.MouseLeave:Connect(function()
		local Info = TweenInfo.new(
			0.2,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.InOut,
			0,
			false,
			0
		)

		local Tween = TweenService:Create(Button, Info, {Rotation = 0})
		Tween:Play()
	end)
end)


--Open/Close Menu
local Button = script.Parent
local Menu = Button.Parent.Menu

Button.MouseButton1Click:Connect(function()
	if Menu.Visible == false then
		Menu.Visible = true
		Button.Text = "Close"
	else
		Menu.Visible = false
		Button.Text = "Menu"
	end
end)

Well, you have a MouseEnter and MouseLeave function inside another MouseEnter function, which I’m not sure what the point of that is. So basically what your code is doing is connecting it when you hover over it the first time and waits until your mouse hovers and leaves the button, and that is why it doesn’t work the first time you hover over it, so remove the first MouseEnter function so it would be:

local Button = script.Parent
local woosh = script.Woosh
local player = game.Players.LocalPlayer
local TweenService = game:GetService("TweenService")

Button.MouseEnter:Connect(function()
	local Info = TweenInfo.new(
		0.2,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.InOut,
		0,
		false,
		0
	)

	local Tween = TweenService:Create(Button, Info, {Rotation = 15})
	Tween:Play()
	woosh:Play()
end)

Button.MouseLeave:Connect(function()
	local Info = TweenInfo.new(
		0.2,
		Enum.EasingStyle.Sine,
		Enum.EasingDirection.InOut,
		0,
		false,
		0
	)

	local Tween = TweenService:Create(Button, Info, {Rotation = 0})
	Tween:Play()
end)


--Open/Close Menu
local Button = script.Parent
local Menu = Button.Parent.Menu

Button.MouseButton1Click:Connect(function()
	if Menu.Visible == false then
		Menu.Visible = true
		Button.Text = "Close"
	else
		Menu.Visible = false
		Button.Text = "Menu"
	end
end)
2 Likes

Thanks! Also, is your script a good non-repetitive way for something like this? I feel like I could make a shortened version using a true boolean for the reverses part.

Hm, could you elaborate on that a bit? I don’t quite understand.

I have no idea how to elaborate this, but I’ll try my best.
Is there a way to make a script that fits both MouseEnter and MouseLeave?

Like,

if MouseIsEntered == true then
    MouseIsEntered = false
    Button.Position = ogPos
else
    MouseIsEntered = false
    Button.Position = newPos

Also, I just tested it, and it didn’t work?

Do you have any errors. If so please show them here.

No, I have no errors. It just doesn’t work the first time, but does the other times

That’s strange, when I tried it, it worked perfectly fine. Maybe there is something I missed.

Edit: No, it worked perfectly fine for me, can you take a picture of your script? There is no reason why it isn’t working the first time.

Wait, I just copied and pasted your version and it worked. I’ll show you mine anyway if you find anything.

1 Like

Oh wait a minute.
Wait, be right back let me try something

It was the MouseEnter duplicate thing. I don’t know why I made it like that.

It’s because you have MouseEnter event twice, first time it just states that that other MouseEnter exists, simply remove that button.mouseneter inside the first mouseenter function and just keep the data and you should be good.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.