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

Code not changing debounce?

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

2 comments

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

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.

1 answer

✓ Accepted answer

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 

2 comments

0

Basically just copy your code into a local script under the "StarterPlayerScripts" folder. I tested it, and your code works.

0

Thank you so much.

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