Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

For loop waits until functions finishes?

Asked by 2 years ago

So I'm making a train and I want the doors to open but it opens the left door first then the for loop waits for that to open and close then open the right door after and I want both of them to open both at once :/

Main Script to activate Event

while true do

    Event:Fire() -- Fires It

    wait(5)
    toGate:Play()
    toGate.Completed:Wait()
    Event:Fire()
    wait(5)
    toMall:Play()
    toMall.Completed:Wait()
end

Doors Opening Script

local this = script.Parent

local TweenService = game:GetService("TweenService")

local TrainDoor = this.Carriage.TrainDoor
local Event = this.OpenDoors

local doortweenInfo = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, 0, false, 0)

function Doors(v)
    if v.Name == "DoorL" then
        print("DoorL")
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosOpen.CFrame}):Play()
        print("Opened")
        wait(5)
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosClosed.CFrame}):Play()
        print("Closed")
    end
    if v.Name == "DoorR" then
        print("DoorR")
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosOpen.CFrame}):Play()
        print("Opened")
        wait(5)
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosClosed.CFrame}):Play()
        print("Closed")
    end
end
Event.Event:Connect(function()
    for i,v in pairs(TrainDoor:GetChildren()) do
        print("Activated!!")
        Doors(v)
    end 
end)

Please help :(

1 answer

Log in to vote
1
Answered by 2 years ago

Use delay:

function Doors(v)
    if v.Name == "DoorL" then
        print("DoorL")
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosOpen.CFrame}):Play()
        print("Opened")
        delay(5, function()
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosClosed.CFrame}):Play()
        end)
        print("Closed")
    end
    if v.Name == "DoorR" then
        print("DoorR")
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosOpen.CFrame}):Play()
        print("Opened")
        delay(5, function()
        TweenService:Create(v, doortweenInfo, {CFrame = v.PosClosed.CFrame}):Play()
        end)
        print("Closed")
    end
end
0
Thanks Brioche_Noodle 45 — 2y
Ad

Answer this question