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

Why is my function not responding to disconnect()?

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

✓ Accepted answer

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

1 comment

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

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