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

Using a coroutine to avoid yielding when multiple players call function, but coroutine still yields?

Asked by
pwnd64 106
3 years ago

Hello, I was told here earlier about coroutines and I'm trying to use one so that each individual player can reload at the same time without the wait(reloadtime) getting in the way.

A solution offered to me was to change the bottom line to reloadevent.OnServerEvent:Connect(coroutine.wrap(reload)) but that threw a dead coroutine error every time, so I made the coroutine within the reload function. However, this seems to be bypassing the intended effect because now there is no error but the coroutine is ineffective.

I am really just totally lost.

local function reload(player, gun)

    local a = coroutine.wrap(function() --make coroutine locally so its unique each time

        local active = gun.gunstats.Active
        if active.Value == true and reloaddebounce == false then
            reloaddebounce = true
            local reloadtime = gun.gunstats.Reloadtime
            local magammo = gun.gunstats.Mag
            local maximum = gun.gunstats.Maximum
            local reloadtick = 0
            if magammo.Value < maximum.Value and tick() - reloadtick >= reloadtime.Value then --cant reload when full

                reloadtick = tick()

                changetoreload:FireClient(player)
                changecursortoreload:FireClient(player)

                    magammo.Value = 0

                gun.phalanx.Magazine.Transparency = 1   
                gun.Handle.Reloadsound:Play()

                    wait(reloadtime.Value)

                reloaddebounce = false
                gun.phalanx.Magazine.Transparency = 0

                    magammo.Value = maximum.Value

            end
        end 
    end)
    a()
end
reloadevent.OnServerEvent:Connect(reload)

Answer this question