Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Azuc 112
6 years ago
Edited 6 years ago

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.

01local Player = game.Players.LocalPlayer
02local Mouse = Player:GetMouse()
03local Input = game:GetService("UserInputService")
04local Sprinting = Player.PlayerScripts:WaitForChild("Core").Sprinting
05 
06local Stamina = 100
07local Debounce = false
08 
09 
10 
11 
12Input.InputBegan:Connect(function(input)
13    if(input.KeyCode==Enum.KeyCode.Space) then
14        Stamina = Stamina - 7
15        print(Stamina) --Debugging Purposes
View all 56 lines...
0
If my answer satisfies your question, please accept it. RAYAN1565 691 — 6y
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. RAYAN1565 691 — 6y

1 answer

Log in to vote
2
Answered by
RAYAN1565 691 Moderation Voter
6 years ago
Edited 6 years ago

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":

01local player = game.Players.LocalPlayer
02local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
03local input = game:GetService("UserInputService")
04local leftShift, space = 0, 0
05local t, f = true, false
06 
07input.InputBegan:Connect(function(key)
08    if (key.KeyCode == Enum.KeyCode.LeftShift) then
09        leftShift = t
10    elseif (key.KeyCode == Enum.KeyCode.Space) then
11        space = t
12    end
13end)
14 
15input.InputEnded:Connect(function(key)
View all 50 lines...

"walkSpeedScript":

01local player = game.Players.LocalPlayer
02local humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
03local input = game:GetService("UserInputService")
04local leftShift, space = 0, 0
05local t, f = true, false
06 
07input.InputBegan:Connect(function(key)
08    if (key.KeyCode == Enum.KeyCode.LeftShift) then
09        leftShift = t
10    elseif (key.KeyCode == Enum.KeyCode.Space) then
11        space = t
12    end
13end)
14 
15input.InputEnded:Connect(function(key)
View all 45 lines...
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 Azuc 112 — 6y
0
Yeah I tried it and its still having the same issue, I honestly have no idea why at this point. Azuc 112 — 6y
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. Azuc 112 — 6y
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. RAYAN1565 691 — 6y
Ad

Answer this question