#Help with closing a coroutine

1 messages · Page 1 of 1 (latest)

torpid gulch
#

im new with coroutine and i thought i would be fit for what im doing. i have a function that lowers a gauge progresively over time, but when the player dies the gauge should lowering and remain. For this i made a function to lower it, and call it when i need. when the player dies, the .Died Event is fired and the coroutine should close. At least that was my idea but the coroutine doesn't close i think the code i made is clearly not what should be done

local Deplete = nil

LocalPlayer.CharacterAdded:Connect(function()
    local Deplete = nil --Reset the variable on death
end)

--The function to lower the gauge
function DepleteGauge(Rate)
    local Gauge = LocalPlayer.UltGauge
    Gauge.Value = 99
    for i = 0,98 do
        task.wait(0.5)
        Gauge.Value = Gauge.Value - Rate
    end
end

function ArchAngel()
--code [...]
Deplete = coroutine.create(DepleteGauge(1))
end

--Test if the gauge is depleting, if yes stop it
LocalPlayer.Character.Humanoid.Died:Connect(function()
    if Deplete ~= nil then
        coroutine.close(Deplete)
    end
end)```
bronze lake
#

Coroutines dont work in parrallel

#

You should create a variable to see of a player is dead

#

And check of the variable is true in the loop

torpid gulch
#

wouldn't that slow the ticking down?

bronze lake
#

Then use the event to set it to true

bronze lake
torpid gulch
#

alr ill try it

bronze lake
#

Its microscopic change

#

Make sure to set it to false in character added

torpid gulch
#

yeap

bronze lake
#

Uhh

#

You also have to start the coroutine

#

coroutine.resume

torpid gulch
#

coroutine is not usefull if i use a condition i can just call the function normally no?

bronze lake
#

Yes

torpid gulch
#

okay i think its working but i have some probleme somewhere else

bronze lake
#

Unless your planning to run multiple function concurrently

wispy sundialBOT
#

studio** You are now Level 6! **studio

torpid gulch