✓ Accepted answer
When using a debounce, you must satisfy all four parts:
- Index a variable and set it to
false (or true 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 to false. 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
2 comments
If my answer satisfies your question, please accept it.
I have made this into a personal project and created a ScreenGui that shows stamina and alters it. If you would like a copy of it, I'd be happy to share with you.