So, i have this function connected via Left Shift. It activates every time you press that key. If you rapidly press or release Left Shift, not sprinting will regenerate more stamina and sprinting will wear off stamina faster, accordingly, until you stop pressing or releasing the button rapidly.
I haven't been able to solve this predicament to date, although i do have a clue: The sprinting value appears to be true or false when the loop is to check the conditions, depending on what you do rapidly. Even adding debounce didn't help.
Here's the script, with added comments:
local function Sprint(_,state)
if state == Enum.UserInputState.Begin then --Sprint
if script.Parent.Stamina.Value < 15 then return end --Minimum stamina needed
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 29
script.Parent.Sprinting.Value = true --Sprinting
script.Parent.Stamina.Value = script.Parent.Stamina.Value - 3
while script.Parent.Stamina.Value > 0 and script.Parent.Sprinting.Value do --Drain stamina
script.Parent.Stamina.Value = script.Parent.Stamina.Value - 1
wait(0.15)
end
else --Stop sprinting (releasing left shift)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 12
script.Parent.Sprinting.Value = false --Not sprinting
wait(1)
while script.Parent.Stamina.Value <= 100 and not script.Parent.Sprinting.Value do --Regen stamina
wait(0.5)
script.Parent.Stamina.Value = script.Parent.Stamina.Value + 1
end
end
end
game.ContextActionService:BindAction("Sprint", Sprint, false, Enum.KeyCode.LeftShift)

1 comment
The main idea solved it nicely. Thank you BlueTaslem, and good luck for 3k!