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

Why is the input connection not being cancelled?

I'm making a lootbox and when the player presses e while near the box they will get something. However, the connection will not be cut off in this script:


local uis = game:GetService("UserInputService")
local InputConnection 
workspace.LootBox.AREA.Touched:Connect(function()
	print("entered")
	workspace.LootBox.Part.BillboardGui.Enabled = true
	InputConnection = uis.InputBegan:Connect(function(input) 
            if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" then
	print("THIS WORKS IF PRINTED")
	
	end
	end)
	end)
	
		workspace.LootBox.AREA.TouchEnded:Connect(function()
		
			workspace.LootBox.Part.BillboardGui.Enabled = false
			print("left")
			InputConnection:Disconnect()
			
		

		end)

Even after leaving the area the player can press e and print the message.

1 comment

0

Is the script a LocalScript? UserInputService doesn't work on ServerScripts.

1 answer

✓ Accepted answer

you could always try and add a variable to check if the player is in the area, so it might look like this

local uis = game:GetService("UserInputService")
local inArea = false
local InputConnection 
workspace.LootBox.AREA.Touched:Connect(function()
	inArea = true
    print("entered")
    workspace.LootBox.Part.BillboardGui.Enabled = true
    InputConnection = uis.InputBegan:Connect(function(input) 
            if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" and inArea == true then
    print("THIS WORKS IF PRINTED")

    end
    end)
    end)

        workspace.LootBox.AREA.TouchEnded:Connect(function()

            workspace.LootBox.Part.BillboardGui.Enabled = false
			inArea = false
            print("left")
            InputConnection:Disconnect()



        end)

1 comment

1

Sorta like a debounce... Thanks! This will open my mind to work more with variables in the future

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