Answered by
6 years ago Edited 6 years ago
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
:
"staminaScript":
01 | local player = game.Players.LocalPlayer |
02 | local humanoid = game.Players.LocalPlayer.Character:FindFirstChild( "Humanoid" ) |
03 | local input = game:GetService( "UserInputService" ) |
04 | local leftShift, space = 0 , 0 |
05 | local t, f = true , false |
07 | input.InputBegan:Connect( function (key) |
08 | if (key.KeyCode = = Enum.KeyCode.LeftShift) then |
10 | elseif (key.KeyCode = = Enum.KeyCode.Space) then |
15 | input.InputEnded:Connect( function (key) |
16 | if (key.KeyCode = = Enum.KeyCode.LeftShift) then |
18 | elseif (key.KeyCode = = Enum.KeyCode.Space) then |
24 | if leftShift = = t and script.Stamina.Value > = 6 then |
25 | script.Stamina.Value = script.Stamina.Value - 6 |
27 | elseif leftShift = = t and script.Stamina.Value < 6 then |
28 | script.Stamina.Value = script.Stamina.Value + 1 |
30 | elseif leftShift = = f and script.Stamina.Value < 100 then |
31 | script.Stamina.Value = script.Stamina.Value + 1 |
35 | if space = = t and script.Stamina.Value > = 7 then |
36 | script.Stamina.Value = script.Stamina.Value - 7 |
38 | elseif space = = t and script.Stamina.Value < 7 then |
39 | script.Stamina.Value = script.Stamina.Value + 1 |
41 | elseif space = = f and script.Stamina.Value < 100 then |
42 | script.Stamina.Value = script.Stamina.Value + 1 |
"walkSpeedScript":
01 | local player = game.Players.LocalPlayer |
02 | local humanoid = game.Players.LocalPlayer.Character:FindFirstChild( "Humanoid" ) |
03 | local input = game:GetService( "UserInputService" ) |
04 | local leftShift, space = 0 , 0 |
05 | local t, f = true , false |
07 | input.InputBegan:Connect( function (key) |
08 | if (key.KeyCode = = Enum.KeyCode.LeftShift) then |
10 | elseif (key.KeyCode = = Enum.KeyCode.Space) then |
15 | input.InputEnded:Connect( function (key) |
16 | if (key.KeyCode = = Enum.KeyCode.LeftShift) then |
18 | elseif (key.KeyCode = = Enum.KeyCode.Space) then |
23 | function walkSpeedProc() |
24 | if leftShift = = t and script.Parent.Stamina.Value < 6 then |
25 | humanoid.WalkSpeed = 16 |
26 | humanoid.JumpPower = 0 |
27 | elseif leftShift = = t and script.Parent.Stamina.Value > = 6 then |
28 | humanoid.WalkSpeed = 25 |
29 | elseif leftShift = = f then |
30 | humanoid.WalkSpeed = 16 |
33 | if space = = t and script.Parent.Stamina.Value > = 7 then |
34 | humanoid.JumpPower = 50 |
35 | elseif space = = t and script.Parent.Stamina.Value < 7 then |
36 | humanoid.JumpPower = 0 |
37 | elseif space = = f then |
38 | humanoid.JumpPower = 0 |