How to make a tween overlap another tween

I need to make a opening tween overlap a closing tween if the closing tween is playing.
I’m making a door and I’m using TweenService to get the doors to open and close smoothly.
I have a problem where the door closes. The doors don’t really open back up right if the player is near it while the door is still closing.
I’ve tried using reference from my other game which used tweens for GUI. Since GUI can overlap a tween, I’m sure that it could work with parts.

I’m using two scripts for this as one is a module and one is a server script.

ServerScript:

local function openDoors()
	if not debounce then
		debounce = true
		tweeningModule.Tween(doorL, {CFrame = openPosL}, .5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0, false)
		tweeningModule.Tween(doorR, {CFrame = openPosR}, .5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0, false)
	end
end

local function closeDoor()
	if debounce then
		debounce = false
		tweeningModule.Tween(doorL, {Position = closePosL}, .5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0, false)
		tweeningModule.Tween(doorR, {Position = closePosR}, .5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0, false)
	end
end

-- Events

doorHitbox.Touched:Connect(function(hit)
	if Players:GetPlayerFromCharacter(hit.Parent) then
		openDoors()
		wait(2)
		closeDoor()
	end
end)

ModuleScript:

local tweenService = game:GetService("TweenService")

local module = {}

function module.Tween(object, goal, duration: number, easingStyle, easingDirection, repeatCount, reverse, delayTime, waitUntilCompleted: boolean)
	local tweenInfo = TweenInfo.new(
		duration,
		easingStyle,
		easingDirection,
		repeatCount,
		reverse,
		delayTime
	)
	local tween = tweenService:Create(object, tweenInfo, goal)

	if object:FindFirstChild("Tween") then
		object.Tween:Destroy()
	end

	tween.Parent = object
	tween:Play()

	if waitUntilCompleted == true then
		tween.Completed:Wait()
		tween:Destroy()
	else
		task.spawn(function()
			tween.Completed:Wait()
			tween:Destroy()
		end)
	end
end

return module

Try adding a debounce within your Touched event handler instead of your closing/opening functions. Because there is no debounce there, this could potentially run multiple times while the doors are still being animated, causing anomalous results.

local animatingDoors = false

doorHitbox.Touched:Connect(function(hit)
	if Players:GetPlayerFromCharacter(hit.Parent) and not animatingDoors then
        animatingDoors = true
		openDoors()
		wait(2)
		closeDoor()
        animatingDoors = false
	end
end)

No need for any other debounces. :slight_smile:

EDIT: I noticed the last parameter in your module.Tween function (waitTillCompleted) is being set to false from your function calls. This will cause it to disregard the state of the tweens, and will end the debounce immediately. Specifying that parameter to true should also solve your issue

Thanks for the help! The doors work nicely now! I’ll be improving them if I can.

1 Like

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