Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I've tried every way I can think of and still not luck, so how can I Debounce this correctly?

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)

2 comments

0

If my answer satisfies your question, please accept it.

0

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.

1 answer

✓ Accepted answer

When using a debounce, you must satisfy all four parts:

  1. Index a variable and set it to false (or true if you want but we'll focus on the former).
  2. 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.
  3. 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.
  4. 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

4 comments

0

I read this over and made the changes necessary but I run into the same issue but just in reverse now if I spam shift the stamina just shoots down forever extremely fast

0

Yeah I tried it and its still having the same issue, I honestly have no idea why at this point.

0

Yeah im at a complete loss at this point, maybe I need to rewrite it all from scratch. At the moment when you run it after you sprint the first time there is a huge delay before it lets you sprint again even though you have the stamina.

1

I wrote up a new script that should solve all your frustrations. Basically, I just set each key to either true or false. Then after that I ran a loop function that checks which keys are set to true and which set to false and changes properties (such as stamina and walkspeed) accordingly.

This discussion is preserved as it was. New answers, comments, and votes are closed.