How do you move multiple objects in a script at different speeds?

Hello again, thank you kindly for the help on the prior issues I had with multi-threading.
I tried to use it again here, but since I’m moving all 3 of the objects at different rates, and one isn’t rotating, how do you run all of these movements at once, instead of back-to-back?

Current script below runs fine, but runs the movements back to back since it’s waiting for the loops to finish.

-- Variables
TableQD = workspace.TowerSystems.TableQD
LowerDoor = TableQD.LowerDoor
UpperDoor = TableQD.UpperDoor
QD = TableQD.QD


local function OpenUpper()
	for i = 1, 12 do
		wait(0.01)
		UpperDoor:SetPrimaryPartCFrame(UpperDoor.PrimaryPart.CFrame * CFrame.Angles(math.rad(-3.1),0,0))
	end
end
local function OpenLower()
	for i = 1, 12 do
		wait(0.01)
		LowerDoor:SetPrimaryPartCFrame(LowerDoor.PrimaryPart.CFrame * CFrame.Angles(math.rad(-2.2),0,0))
	end
end
local function ExtendQD()
	for i = 1,15 do
		wait(0.01)
		QD:SetPrimaryPartCFrame(QD.PrimaryPart.CFrame * CFrame.new(0.5,0,0))
	end
end

OpenLower()
OpenUpper()
ExtendQD()
wait(1)
script.Disabled = true

You should use coroutines to run each at the same time.

How would I go about starting this, do coroutines work similar to functions, where I could do something such as the below?

local function OpenQD()
   coroutine.resume(coroutine.create(OpenUpper,OpenLower,ExtendQD)
end

OpenQD()

I believe you would have to do it like this:

local function OpenQD()
   coroutine.resume(coroutine.create(function()
      OpenUpper()
      OpenLower()
      ExtendQD()
   end))
end

OpenQD()

You can just do the following…

coroutine.wrap(OpenUpper)() 
coroutine.wrap(OpenLower)() 
coroutine.wrap(ExtendQD)() 

Also I recommend you use task.wait() opposed to you currently using wait(), considering wait() is becoming or is deprecated and considered not a great practice now. Hopefully that helps :+1:

1 Like