local stamina = Instance.new("IntValue") stamina.Name = "Stamina" stamina.Value = 100 stamina.Parent = script.Parent local running = false local humanoid = script.Parent.Humanoid function regen () while true do while stamina.Value < 100 and running == false do print("Regenerating..", stamina.Value) wait(0.1) stamina.Value = stamina.Value + 1 end stamina.Changed:Wait() end end spawn(regen) function onKeyPress(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then print("Running") running = true end if userInputState == Enum.UserInputState.End then print("Walking") running = false end end game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.LeftShift)
This is a test script i made for sprinting so not all parts is included but still works the same way, this is a local script inside the StarterCharacterScript. The concept is that while I'm not running I'm regenerating 1/100 stamina every second if my stamina is not 100, while running I'm loosing 2/100 stamina every second. The problem is when I started running my stamina won't regenerate (which is how it's supposed to work)
but when i stopped running my stamina still doesn't regenerate
, when I change the value of the stamina to something lower than 100 via explorer it starts to regenerate again.
I put this exact code into the script and it works fine for me. (Probably could be better, but it works.)
function onKeyPress(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then running = true while true do wait(0.1) stamina.Value = stamina.Value - 2 end end if userInputState == Enum.UserInputState.End then running = false while true do wait(0.1) stamina.Value = stamina.Value + 2 end end end