Can someone PLEASE explain how to use a coroutine with arguments

ONLY ISSUE: I don’t know how to make the function call without an ERROR
I know I’m just using the coroutines wrong here so someone can probably give me a quick solution I hope. I literally couldn’t find a single decent example of someone doing this WITH arguments in the function I know it should be really simple.

local paddleFlash = coroutine.create(function(pad,transp)
	local oTime = time()
	pad.Transparency = 0.25
	while true do
		task.wait()
		local cTime = time()
		local dt = cTime - oTime
		print(dt)
		pad.Transparency = pad.Transparency + (dt*0.2)
		if pad.Transparency >= transp then
			pad.Transparency = transp
			break
		end
	end
end)

local function ComputerHitBall()
	coroutine.resume(paddleFlash(PCPad,PCPad.Transparency))
end
2 Likes

What you’re doing here is trying to call paddleFlash and then trying to use coroutine on the result.
This is how you should use arguments with coroutines:

coroutine.resume(paddleFlash, PCPad, PCPad.Transparency)
3 Likes

Of course it’s THAT easy thanks man also I think breaking the loop seems to make the function unusable after?

I’m not actually sure. I think you need to create a new coroutine each time:

local function paddleFlash(pad,transp)
	pad.Transparency = 0.25
	while true do
		local dt = task.wait()
		print(dt)
		pad.Transparency = pad.Transparency + (dt*0.2)
		if pad.Transparency >= transp then
			pad.Transparency = transp
			break
		end
	end
end

local function ComputerHitBall()
	coroutine.resume(coroutine.create(paddleFlash), PCPad, PCPad.Transparency)
end

Also I modified the dt variable for testing because for some reason cTime - oTime kept giving me 0 on my computer.

That is WEIRD what’s even WEIRDER is when I try your method of making the coroutine each time the function gets called every time, BUT now the while true do loop never runs! So it just stops before???

update fixed via this:

local function ComputerHitBall()
	local co = coroutine.create(function()
		paddleFlash(PCPad,PCPad.Transparency)
	end)
	coroutine.resume(co)
end
1 Like

So just to clarify, it is working fine now?

yessir thank you for all the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.