Basically spamming left shift will cause the InputEndeds stamina regen function to repeat for each time you let go of the button allowing you to regen stamina at an insane rate.
I have tried every placement of debounce I can think of and still hasnt changed a thing.
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local Input = game:GetService("UserInputService") local Sprinting = Player.PlayerScripts:WaitForChild("Core").Sprinting local Stamina = 100 local Debounce = false Input.InputBegan:Connect(function(input) if(input.KeyCode==Enum.KeyCode.Space) then Stamina = Stamina - 7 print(Stamina) --Debugging Purposes end end) Input.InputBegan:Connect(function(input) if(input.KeyCode==Enum.KeyCode.LeftShift) then Sprinting.Value = true local Humanoid = game.Players.LocalPlayer.Character.Humanoid if Humanoid then Humanoid.WalkSpeed = 25 end while Sprinting.Value == true and Stamina >= 6 do if Stamina >=6 then Stamina = Stamina - 1 wait(.05) end print(Stamina) --Debugging Purposes end Humanoid.WalkSpeed = 16 Sprinting.Value = false end end) Input.InputEnded:Connect(function(input) if(input.KeyCode==Enum.KeyCode.LeftShift) then Debounce = true Sprinting.Value = false local Humanoid = game.Players.LocalPlayer.Character.Humanoid if Humanoid then Humanoid.WalkSpeed = 16 end while Sprinting.Value == false and Stamina <=99 and Debounce == true do if Stamina <=99 then Stamina = Stamina + 1 wait(.7) print(Stamina) --Debugging Purposes end end Debounce = false end end)
When using a debounce, you must satisfy all four parts:
- Index a variable and set it to
false
(ortrue
if you want but we'll focus on the former).- Within the if-then statement, check for whether the variable is set to false. If it is, the code will continue to run. If not, the code will not execute further.
- Usually the next line after the if-then statement, change the variable to the opposite (in this case, we'll change it to
true
). This prevents the script to run the code again since the variable is no longer equal tofalse
. Thus the if-then statement fails and the rest of the code will not run which is what we want. We want the code to run once and not multiple times.- After the code has executed completely, we change the variable back to
false
.
The above information is useful in certain scripts. But I can recreate two scripts: one for walkspeed and one for stamina, that does not need to utilize debounce.
The scripts below is written in a way that doesn't need to use debounce and is less complicated than the script you attempted to write.
These scripts should appear in the following order with the following names under StarterGui
:
"StarterGui" (StarterGui)
"staminaScript" (LocalScript)
"Stamina" (IntValue) Set the value to '100' in Studio
"walkSpeedScript" (LocalScript)
"staminaScript":
local player = game.Players.LocalPlayer local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid") local input = game:GetService("UserInputService") local leftShift, space = 0, 0 local t, f = true, false input.InputBegan:Connect(function(key) if (key.KeyCode == Enum.KeyCode.LeftShift) then leftShift = t elseif (key.KeyCode == Enum.KeyCode.Space) then space = t end end) input.InputEnded:Connect(function(key) if (key.KeyCode == Enum.KeyCode.LeftShift) then leftShift = f elseif (key.KeyCode == Enum.KeyCode.Space) then space = f end end) function staminaProc() if leftShift == t and script.Stamina.Value >= 6 then script.Stamina.Value = script.Stamina.Value - 6 wait(1) elseif leftShift == t and script.Stamina.Value < 6 then script.Stamina.Value = script.Stamina.Value + 1 wait(0.5) elseif leftShift == f and script.Stamina.Value < 100 then script.Stamina.Value = script.Stamina.Value + 1 wait(0.5) end if space == t and script.Stamina.Value >= 7 then script.Stamina.Value = script.Stamina.Value - 7 wait(0.6) elseif space == t and script.Stamina.Value < 7 then script.Stamina.Value = script.Stamina.Value + 1 wait(0.5) elseif space == f and script.Stamina.Value < 100 then script.Stamina.Value = script.Stamina.Value + 1 wait(0.5) end end while true do wait() staminaProc() end
"walkSpeedScript":
local player = game.Players.LocalPlayer local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid") local input = game:GetService("UserInputService") local leftShift, space = 0, 0 local t, f = true, false input.InputBegan:Connect(function(key) if (key.KeyCode == Enum.KeyCode.LeftShift) then leftShift = t elseif (key.KeyCode == Enum.KeyCode.Space) then space = t end end) input.InputEnded:Connect(function(key) if (key.KeyCode == Enum.KeyCode.LeftShift) then leftShift = f elseif (key.KeyCode == Enum.KeyCode.Space) then space = f end end) function walkSpeedProc() if leftShift == t and script.Parent.Stamina.Value < 6 then humanoid.WalkSpeed = 16 humanoid.JumpPower = 0 elseif leftShift == t and script.Parent.Stamina.Value >= 6 then humanoid.WalkSpeed = 25 elseif leftShift == f then humanoid.WalkSpeed = 16 end if space == t and script.Parent.Stamina.Value >= 7 then humanoid.JumpPower = 50 elseif space == t and script.Parent.Stamina.Value < 7 then humanoid.JumpPower = 0 elseif space == f then humanoid.JumpPower = 0 end end while true do wait() walkSpeedProc() end