Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Coroutine is not resuming after yield?

Asked by
Qorm 100
9 years ago
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

Log in to vote
2
Answered by 9 years ago

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.

Ad

Answer this question