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

Why is my function not responding to disconnect()?

Asked by
Warphi 51
4 years ago

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

01if 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)
10end

thank you for your time

1 answer

Log in to vote
0
Answered by 4 years ago

My advice is to put the function and variables at the top instead of using it at the bottom.

01local connection
02local function input(key) --Pick a better name.
03    if key.KeyCode = Enum.KeyCode.E then
04    print("Hello")
05    connect:Disconnect()
06end
07 
08if active and v.Door then
09    connection = UIService.InputBegan:Connect(input)
10end

If that won't work, then I suggest using a debounce.

01local function input(key)
02    if key.KeyCode == Enum.KeyCode.E then
03        print("Hello")
04    end
05end
06local debounce = false
07if 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
14end
0
hi, thanks for the input but when tested they came out with the same results. i did however remove it from the repeat until loop that encompasses most of my script and it worked fine. thank you again Warphi 51 — 4y
Ad

Answer this question