How can I submit arguments into a coroutine?

I made a coroutine so that I can create a seperate line of code that preforms a task for me, so that the next line of code after that does not have to yield what its doing due to the multiple task.wait()'s being used.

How would I put in arguments to a coroutine?

Here’s the code

function effect(marble)
	local particle = RainbowParticle:Clone()
	particle.Parent = marble

	task.wait(2)

	particle.Enabled = false
	game:GetService("Debris"):AddItem(particle, 12)
end

local effectRoutine = coroutine.create(effect)
	local Velocity = Instance.new("BodyAngularVelocity")
	Velocity.Parent = marble
	local Hum = char:WaitForChild("Humanoid")
	local Weld = Instance.new("Weld")
	Weld.Parent = marble
	Weld.Part0 = root
	Weld.Part1 = marble
	Hum.PlatformStand = true
	
	coroutine.resume(effectRoutine)

	while task.wait() do
		if marble then
			marble:WaitForChild("BodyAngularVelocity").AngularVelocity = Vector3.new(char.Humanoid.MoveDirection.z * 48, 0, char.Humanoid.MoveDirection.x * -48)
			marble:WaitForChild("BodyAngularVelocity").MaxTorque = Vector3.new(30000,30000,30000)
			if char.Humanoid.MoveDirection == Vector3.new(0,0,0) then
				marble:WaitForChild("BodyAngularVelocity").MaxTorque = Vector3.new(0,0,0)
			end
		elseif not marble then
			break
		end
	end

You can use the task library instead, which allows you to pass the arguments for the function/thread.

local function f(arg)
	task.wait(1)
	print(arg)
end

task.spawn(f, "hello")

You can also use this trick to replace Debris with, since this implementation won’t throttle.

-- game.Destroy is an alias for the Instance:Destroy method
-- we pass particle as the self argument so it acts like
-- particle:Destroy()
task.delay(12, game.Destroy, particle)
1 Like

you can first create the thread, then resume it like this
local co = coroutine.create(func)
coroutine.resume(co,arg1,arg2,yadda,yadda)