I have a script that opens two different doors at the same time, like an elevator.
local function Open()
if OpenValue == true then return end
local RightSlideTween = TweenService:Create(RightRoot, SlideInfo, {CFrame = RightRoot.CFrame * CFrame.new(RightRoot.Size.X + 0.0001, 0, 0)})
RightSlideTween:Play()
local LeftSideTween = TweenService:Create(LeftRoot, SlideInfo, {CFrame = LeftRoot.CFrame * CFrame.new(LeftRoot.Size.X + 0.0001, 0, 0)})
LeftSideTween:Play()
RightSlideTween.Completed:Wait()
print("Tween 1 Complete")
LeftSideTween.Completed:Wait()
print("Doors Open! Function complete!")
end
Unfortunately, only the first print is written in the console, “Tween 1 Complete” and the other one never pops up. Am I doing something wrong or is this a Roblox bug?
local LeftSideTween = TweenService:Create(LeftRoot, SlideInfo, {CFrame = LeftRoot.CFrame * CFrame.new(LeftRoot.Size.X + 0.0001, 0, 0)})
local RightSideTween = TweenService:Create(RightRoot, SlideInfo, {CFrame = RightRoot.CFrame * CFrame.new(RightRoot.Size.X + 0.0001, 0, 0)})
local function OpenLeftDoor()
LeftSideTween:Play()
LeftSideTween.Completed:Wait()
return true
end
local function OpenRightDoor()
RightSideTween:Play()
RightSideTween.Completed:Wait()
return true
end
local function Open()
if OpenRightDoor() and OpenLeftDoor() then
print("Door has been opened")
---- more code ---------
end
end
So that you can call the tweens at any point from anywhere in the script to see what state they’re in. If something else happens and you need to immediately stop the door from opening/closing you can just do Tween:Cancel()
When doing :Wait() functions, it’s a good habit to put a 0 in between the parentheses so there isn’t a small extra delay after it’s completed. But just re order the code so that you have all the tweens at the top of the code and then play one at a time and wait for one at a time.
The second tween has already completed before you execute Wait so it’s going to wait forever. Check to make sure it’s playing before you wait on it…
if LeftSideTween.PlaybackState == Enum.PlaybackState.Playing then
LeftSideTween.Completed:Wait()
end
This solution by far works the best for me since I wanted two doors to open at the same time. Thank you so much for the other solutions guys! Much appreciated