Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

For loop waits until functions finishes?

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

✓ Accepted answer

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

1 comment

0

Thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.