How can I make this tween text script efficient

I have a function which either tweens the text up or down depending where the mouse is, if it enters the text goes up but if the mouse leaves the box it goes down.

I have a problem with once it enters it takes multiple times for the arrow to go out of the box so that the text can come back down so I need help how I either can reverse it or how I can cancel it or something

script.Parent.MouseEnter:Connect(function()
	TweenNameFrame(script.Parent.Parent.GunType, "up")
end)

script.Parent.MouseLeave:Connect(function()
	print("d")
	TweenNameFrame(script.Parent.Parent.GunType, "down")
end)
function TweenNameFrame(frame, State)
	if State == "up" then
		frame:TweenPosition(UDim2.new(0.015, 0,-0.265, 0))
	else
		frame:TweenPosition(UDim2.new(0.015, 0,0.43, 0))
	end
end
2 Likes

I know that you can cancel a tween that is created by TweenService, since it returns a Tween object when you use :Create(). I don’t know if TweenPosition works in the same way though, so you might be forced to use TweenService.

Example:

local TweenService = game:GetService("TweenService")
 
local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
 
local tween = TweenService:Create(tweenInfo, Position =  UDim2.new(0.015, 0,-0.265, 0)}
 
tween:Play() --play

tween:Cancel() -- cancel

Using this method you could cancel, then play a tween in the other direction.

Another option would be to just change the .Position manually, instead of using tweens. This would only be practical if you are Tweening as simply as linearly, or exponentially.

1 Like