How to use Coroutine to make part grow?

I’m new to Coroutines and threads. I want to make a part spawn small and then use a Coroutine to grow it (so that the Coroutine function doesn’t slow and affect the main function)

Example:

local p = Instance.new("Part")
p.Size = Vector3.new(1, 1, 1)

local thread = coroutine.create(function()
   -- Make part grow here to Vector3.new(5, 5, 5) but take 4 seconds to do so
   -- wait(1)
   -- p.Size = Vector3.new(2, 2, 2)
   -- wait(1)
   -- p.Size = Vector3.new(3, 3, 3)
   -- etc..
end)

coroutine.resume(thread)

So just call coroutine.resume(thread) after the thread function? O_o

Correct, the coroutine will run in its own thread of execution, circumventing the disruption of the main thread.

You can test this by adding a print command directly after resuming the coroutine.

1 Like