Stopping a tween

So when getting close to a part and then going far back and go close again, the close tween would still be playing and the gui would be disabled:

function module.TweenOpen()
	print("open")

	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)
	local tween = TweenService:Create(UIScale, tweenInfo, {Scale = 1})
	tween:Play()
end

function module.TweenClose()
	print("close")

	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
	local tween = TweenService:Create(UIScale, tweenInfo, {Scale = 0})
	tween:Play()

	tween.Completed:Connect(function()
		gui.Enabled = false
		gui.Adornee = nil
	end)
end

I have tried to wrap a coroutine and to close the coroutine when the module.TweenOpen() runs and also tried to create a variable at the start of the script called “tween” but that didn’t work either.

You could have a variable for the tween that is playing and when the function is called check if there is a tween already if so cancel that one and set the current tween to nil, and set it to nil to on the .Completed event

What exactly is the issue? I don’t really understand.

just do tween:pause() (char limit [everyone loves it yayy])

or owerwrite the old tween (char limit [everyone still loves it yayy])

Thats what I tried:

local tween

function module.TweenOpen()
	print("open")
	
	if tween then
		tween:Pause()
		tween = nil
	end

	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)
	tween = TweenService:Create(UIScale, tweenInfo, {Scale = 1})
	tween:Play()
end

function module.TweenClose()
	print("close")

	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
	tween = TweenService:Create(UIScale, tweenInfo, {Scale = 0})
	tween:Play()

	tween.Completed:Connect(function()
		gui.Enabled = false
		gui.Adornee = nil
	end)
end

what happens (char limit [why])

Same thing. the tween.Completed would still run.

just delete the pause (char limit[lol])

Have you also tried using :Cancel() or :Destroy()

2 Likes

if the variable is overwritten its automaticly deleted

so just don’t use pause (char limit[…])

I just tried that but that wouldn’t work.

if im not wrong cancel resets the tween

Have you also tried using :Cancel() or :Destroy()

Whats the issue exactly?


then try cancel (cahr limit[looooooooooooooool])

Yeah i myself would Destroy it but one gotta check the options

I don’t want the tween to reset. When going close to the egg, it would print “done” even though the close tween doesn’t play.

you have to enable the ui again when it opens

and leave the cancel out

Its in a different client script:

local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer

local Eggs = workspace:WaitForChild("Eggs")

local EggDisplayGui = script:WaitForChild("EggDisplayGui")
local EggDisplayModule = require(script:WaitForChild("EggDisplayModule"))

local NearestEgg = script:WaitForChild("NearestEgg")

RunService.Heartbeat:Connect(function()
	local nearestEgg, nearestDistance = EggDisplayModule.GetNearestEgg()

	if nearestEgg and not NearestEgg.Value then
		NearestEgg.Value = nearestEgg

		EggDisplayGui.Adornee = nearestEgg.PrimaryPart
		EggDisplayGui.Enabled = true

		EggDisplayModule.TweenOpen()

	elseif not nearestEgg and NearestEgg.Value then
		NearestEgg.Value = nil
		EggDisplayModule.TweenClose()
	end
end)