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

Coroutine is not resuming after yield?

local player=game.Players.LocalPlayer
mouse=player:GetMouse()
on=false
battery=script.Parent.Parent.battery.power

local csd=coroutine.create(function()
	while on==true and wait(1/6) do
		print("test")
	battery.Size=battery.Size+UDim2.new(-0.01,0,0)
	wait(1.5)
	end
end)

mouse.KeyDown:connect(function(key)
--code
end)

charge=false
script.Parent.Parent.filter.Changed:connect(function(changed)
	if script.Parent.Parent.filter.Visible==true then
		print("charging")
		charge=true
		coroutine.yield(csd)
	else
		print("not charging")
		charge=false
		coroutine.resume(csd)
	end
end)

Why is it doing this? Any help would be appreciated

1 answer

✓ Accepted answer

coroutine.yield does not take a coroutine as its argument - it takes a list of arguments that it returns to whatever calls coroutine.resume. coroutine.yield will always yield the current coroutine.

Another problem is that coroutine.resume won't work on dead threads, and csd will become dead as soon as on becomes false.

In this case, I recommend not creating extra coroutines, since you get a new coroutine every time an event is triggered anyway (ie your Changed function on line 19 starts in its own coroutine). Instead of wrapping your function in a coroutine, just give it a name and call it from your Changed function -- but make sure that the function isn't already running first.

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