I’m trying to make working curtains that open and close in the middle of tweening, whenever the player wants.
The curtains go away if you click them multiple times, while they tween. Here’s an example: example
This only works perfectly if you wait until the tweening finishes, but I don’t want that.
Here’s what I’m trying to achieve (source: Start Survey): example
Here’s my tweening script.
local tweenService = game:GetService("TweenService")
local curtains = script.Parent.Curtains.Value
local leftCurtain = curtains.LeftCurtain
local rightCurtain = curtains.RightCurtain
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = curtains.ClickDetectorPart
local isFolded = false
local defaultSizeX = 3.851
local openedSizeX = 1.32
local playingTweens = { }
local function makeTween(target, newPoint, speed, delta)
local tween = tweenService:Create(
target,
TweenInfo.new(
(target.Position.X - newPoint) / speed,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0, false, 0
),
{
Size = Vector3.new(newPoint, target.Size.Y, target.Size.Z),
Position = Vector3.new(target.Position.X, target.Position.Y, target.Position.Z - delta)
}
)
print(delta)
return tween
end
local function updateTweens()
local tweens =
{
open =
{
openLeftCurtain = makeTween(leftCurtain, openedSizeX, 40, (defaultSizeX - openedSizeX) / 2),
openRightCurtain = makeTween(rightCurtain, openedSizeX, 40, (defaultSizeX - openedSizeX) / 2)
},
close =
{
closeLeftCurtain = makeTween(leftCurtain, defaultSizeX, 40, ((defaultSizeX - openedSizeX) / 2) * -1),
closeRightCurtain = makeTween(rightCurtain, defaultSizeX, 40, ((defaultSizeX - openedSizeX) / 2 * -1))
}
}
return tweens
end
clickDetector.MouseClick:Connect(function()
for i, v in pairs(playingTweens) do --Stops all current tweens from playing
table.remove(playingTweens, i)
v:Destroy()
end
--Plays the new tweens
local tweens = updateTweens()
if isFolded then
isFolded = false
for _, v in pairs(tweens.close) do
table.insert(playingTweens, v)
v:Play()
end
else
isFolded = true
for _, v in pairs(tweens.open) do
table.insert(playingTweens, v)
v:Play()
end
end
print(playingTweens)
end)