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)
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.
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.