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
3 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

if active == true and v.Door then --is localplayer close enough to door
    local connection
    local function whywontthiswork(key) --prints over 300 times per input?
        if key.KeyCode == Enum.KeyCode.E then
            print("hello")
            connection:Disconnect()
        end
    end
    connection = UIService.InputBegan:Connect(whywontthiswork)
end

thank you for your time

1 answer

Log in to vote
0
Answered by 3 years ago

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

local connection
local function input(key) --Pick a better name.
    if key.KeyCode = Enum.KeyCode.E then
    print("Hello")
    connect:Disconnect()
end

if active and v.Door then
    connection = UIService.InputBegan:Connect(input)
end

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

local function input(key)
    if key.KeyCode == Enum.KeyCode.E then
        print("Hello")
    end
end
local debounce = false
if active and v.Door then
    if not debounce then
        debounce = true
        UIService.InputBegan:Connect(input)
        wait()
        debounce = false
    end
end
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 — 3y
Ad

Answer this question