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

Code not changing debounce?

Asked by 4 years ago

I am trying to change the state of the debounce by clicking the shift key. I wrote this code but the if-then line isn't registering the change of state. Is there something wrong in my code or is there a technical thing I just don't know?

Here is my code:

local UserInputService = game:GetService("UserInputService")
local debounce = false

UserInputService.InputEnded:Connect(function(key)
     if key.KeyCode == Enum.KeyCode.LeftShift then
        debounce = false
    end
end)

UserInputService.InputBegan:Connect(function(key)
    if key.Keycode == Enum.KeyCode.LeftShift then
        debounce = true
    end
end)

if debounce == true then
    print ("1")
end
0
You are using a local variable to define debounce. So, when you change it in their own functions, it doesn’t actually change it when you first define it. To the code, debounce doesn’t actually changed outside of the input functions, so it stays false DarkDanny04 407 — 4y
1
@DarkDanny That's wrong. He is declaring a local debounce within the script, so that debounce will be local to everything within that certain script, including it's functions. But if a local variable is declared within a function, that variable will only be usable in that function it was created in. Yozoh 146 — 4y

1 answer

Log in to vote
0
Answered by
Yozoh 146
4 years ago
Edited 4 years ago

Okay, so I think I see what the issue is. Your code works completely fine, but you're using the wrong type of script to be handling userinputservice. I recommended that you handle UserInputService with a local script under the "StarterPlayerScripts" folder nested under StarterPlayer. Player input should always be handled on the client side.

Also on line 11, you need to capitalize the c. You wrote Keycode, and not KeyCode.

if key.Keycode == Enum.KeyCode.LeftShift then 
0
Basically just copy your code into a local script under the "StarterPlayerScripts" folder. I tested it, and your code works. Yozoh 146 — 4y
0
Thank you so much. bobthetomato5 7 — 4y
Ad

Answer this question