Help with MouseEnter, MouseLeave

Hi!
I’m needing some help with the tweening or MouseEnter/Leave.
I’m trying to achieve that when the mouse leaves the “BackpackButton” it returns to the original size & pos, but, when it leaves, it just doesn’t returns to the original pos & size, this only happens when the mouse passes by the button really fast.

local BackpackButton = script.Parent
local Banner = BackpackButton.Parent
local TitleLabel = Banner.TitleLabel

local QuickButtons = Banner.Parent
local PlayerHud = QuickButtons.Parent
local BackpackUI = PlayerHud.BackpackUI

local MouseOverModule = require(game.ReplicatedStorage.MouseOverModule)
local MouseEnter, MouseLeave = MouseOverModule.MouseEnterLeaveEvent(BackpackButton)

MouseEnter:Connect(function()
	Banner:TweenPosition(UDim2.fromScale(-0.007, 0.212), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.09)
	BackpackButton:TweenSize(UDim2.fromOffset(140, 140), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.09)
	TitleLabel.Visible = true
	
	MouseLeave:Connect(function()
		Banner:TweenPosition(UDim2.fromScale(-0.007, 0.342), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.09)
		BackpackButton:TweenSize(UDim2.fromOffset(128, 128), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.09)
		TitleLabel.Visible = false
	end)
end)

BackpackButton.MouseButton1Click:Connect(function()
	if BackpackUI.Visible == false then
		BackpackUI.Visible = true
		BackpackButton.ImageTransparency = 0.6
	else
		BackpackUI.Visible = false
		BackpackButton.ImageTransparency = 0
	end
end)

Example Video:

the easiest way to stop this from happening is to make a seperate variable for the tween outside of the connections

local currentTween = nil

MouseEnter:Connect(function()
    if currentTween then
        currentTween:Stop()
        currentTween = nil
    end

    --[[
        instead of using the built in tweensize and tweenposition use tweenservice
        because tweensize and tweenposition doesnt return a tween object iirc
    ]]

    currentTween = --[[established tween]]
end)

the same could be done with mouseLeave.

P.S. it probably isnt good to connect MouseLeave every single time MouseEnter fires because it wont get garbage collected

this connected function should be outside of the MouseEnter being in there like that makes the connection over and over every time mouseentered is fired

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