I’m making script, which allows run. But the problem is that coroutine.status(StaminaChanger)
is always suspended
, even if it actually running.
local StaminaChanger = coroutine.create(function()
while true do
local Delta = RunService.RenderStepped:Wait()
if Running == true and CurrentStamina > 0 then
CurrentStamina = math.max(CurrentStamina - (RunStaminaUsage * Delta), 0)
if CurrentStamina <= 0 then
Weak = true
Running = false
CurrentSpeedMultiplayer = CurrentSpeedMultiplayer - WeakRunSpeed + WeakWalkspeed
Humanoid.WalkSpeed = BaseWalkSpeed * CurrentSpeedMultiplayer
elseif CurrentStamina <= 200 and Weak == false then
Weak = true
CurrentSpeedMultiplayer = CurrentSpeedMultiplayer - NormalRunSpeed + WeakRunSpeed
Humanoid.WalkSpeed = BaseWalkSpeed * CurrentSpeedMultiplayer
end
PlayerStaminaBarDisplay.Size = UDim2.new(CurrentStamina / MaxStamina, 0, 1, 0)
PlayerStaminaBarQuantityText.Text = tostring(math.round(CurrentStamina * 1000 / MaxStamina) / 10) .. "%"
elseif Running == false and CurrentStamina < MaxStamina then
CurrentStamina = math.min(CurrentStamina + (StaminaRegen * Delta), 1000)
if CurrentStamina > 200 and Weak == true then
Weak = false
CurrentSpeedMultiplayer = CurrentSpeedMultiplayer - WeakWalkspeed + NormalWalkspeed
Humanoid.WalkSpeed = BaseWalkSpeed * CurrentSpeedMultiplayer
end
PlayerStaminaBarDisplay.Size = UDim2.new(CurrentStamina / MaxStamina, 0, 1, 0)
PlayerStaminaBarQuantityText.Text = tostring(math.round(CurrentStamina * 1000 / MaxStamina) / 10) .. "%"
elseif Running == false and CurrentStamina >= MaxStamina then
coroutine.yield()
end
end
end)
local function ToggleRun(actionName, inputState, inputObject)
if actionName == "Run" and inputState == Enum.UserInputState.Begin then
Running = true
print(coroutine.status(StaminaChanger) .. ", start run")
if coroutine.status(StaminaChanger) == "suspended" then
coroutine.resume(StaminaChanger)
end
Humanoid.WalkSpeed = BaseWalkSpeed * CurrentSpeedMultiplayer
elseif actionName == "Run" and inputState == Enum.UserInputState.End then
Running = false
print(coroutine.status(StaminaChanger) .. ", stop run")
if coroutine.status(StaminaChanger) == "suspended" then
coroutine.resume(StaminaChanger)
end
Humanoid.WalkSpeed = BaseWalkSpeed * CurrentSpeedMultiplayer
end
end
can someone explain me, why coroutine.status()
don’t work like needed, and why when I trying resume this coroutine, Delta
became nil?