hi, i'm relatively new to scripting and i'm trying to make a door that opens when you get near it and press E.
i've been having trouble with my inputBegan function where it prints far too many times on one keypress and freezes the game momentarily when printing. Wait() did not work so i tried disconnect, although it seems to do nothing. i do have it in a repeat (.....) until loop, and ive considered this might be a problem, does anybody know?
am i using this wrong? can somebody explain a better way to stop my function from repeating more than once? my code is below
01 | if active = = true and v.Door then --is localplayer close enough to door |
02 | local connection |
03 | local function whywontthiswork(key) --prints over 300 times per input? |
04 | if key.KeyCode = = Enum.KeyCode.E then |
05 | print ( "hello" ) |
06 | connection:Disconnect() |
07 | end |
08 | end |
09 | connection = UIService.InputBegan:Connect(whywontthiswork) |
10 | end |
thank you for your time
My advice is to put the function and variables at the top instead of using it at the bottom.
01 | local connection |
02 | local function input(key) --Pick a better name. |
03 | if key.KeyCode = Enum.KeyCode.E then |
04 | print ( "Hello" ) |
05 | connect:Disconnect() |
06 | end |
07 |
08 | if active and v.Door then |
09 | connection = UIService.InputBegan:Connect(input) |
10 | end |
If that won't work, then I suggest using a debounce.
01 | local function input(key) |
02 | if key.KeyCode = = Enum.KeyCode.E then |
03 | print ( "Hello" ) |
04 | end |
05 | end |
06 | local debounce = false |
07 | if active and v.Door then |
08 | if not debounce then |
09 | debounce = true |
10 | UIService.InputBegan:Connect(input) |
11 | wait() |
12 | debounce = false |
13 | end |
14 | end |