im new to scripting and im making a script for a sprinting mechanic with stamina, the walkspeed does work for running and walking but my stamina is stuck at 99 for some reason.
here is the code
local myMaxStamina = 100
local myStamina = myMaxStamina
local myWalkspeed = 10
local myRunspeed = 25
local AmSprinting = false
local AmGainStam = true
local AmExhaust = false
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
task.wait()
local AmSprinting = true
local AmGainStam = false
Humanoid.WalkSpeed = myRunspeed
while AmSprinting == true and myStamina > 0 do
local myStamina = (myStamina - 1)
print(myStamina)
wait(0.1)
if myStamina <= 0 then
local AmSprinting = false
local AmGainStamina = true
local AmExhaust = true
Humanoid.WalkSpeed = myWalkspeed
wait(0.5)
local AmExhaust = false
end
end
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
task.wait()
local AmSprinting = false
local AmGainStamina = true
Humanoid.WalkSpeed = myWalkspeed
if AmExhaust == true then
repeat
task.wait()
until AmExhaust == false
end
while AmGainStamina == true and AmSprinting == false and AmExhaust == false do
local myStamina = (myStamina + 1)
wait(0.1)
if myStamina == myMaxStamina then
repeat
task.wait()
local AmGainStamina = false
Humanoid.WalkSpeed = myWalkspeed
until AmSprinting == true
end
end
end
end)