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

Why can my Gui still pop up after leaving the area?

I'm making a gui for a npc and when you get close you can press e to open it. It works fine at first but after the first time of leaving the area you can still press e to open the gui! I'm dealing with a local script under StarterPlayerScripts:

 local uis = game:GetService("UserInputService")
local RegionDetector = require(script.RegionDetector)

local NpcRange = RegionDetector:NewRegion(workspace.Tim.HumanoidRootPart.Position, 20)

function NpcRange.EnteredRegion()
	
workspace.E.BillboardGui.Enabled = true

	print("Player has entered the region")
	local InputConnection
		uis.InputBegan:Connect(function(input, gP) if not gP then
    if input.UserInputType.Name == "Keyboard" and
         input.KeyCode.Name == "E" then
	game.Players.LocalPlayer.PlayerGui.TimGui.Frame.Visible = true

	
	
end
	
	end
end)
	
	
end


function NpcRange.LeftRegion()
	workspace.E.BillboardGui.Enabled = false
	game.Players.LocalPlayer.PlayerGui.TimGui.Frame.Visible = false
	game.Players.LocalPlayer.PlayerGui.TimGui.ScrollingFrame.Visible = false
	print("Player has left the region")
end

Anyways to make the UserInputService go away then come back when you step in? I also have a module script under the local script but it just deals with the areas of the npc.

1 answer

✓ Accepted answer

You forgot to disconnect the InputConnection

 local uis = game:GetService("UserInputService")
local RegionDetector = require(script.RegionDetector)

local InputConnection

local NpcRange = RegionDetector:NewRegion(workspace.Tim.HumanoidRootPart.Position, 20)

function NpcRange.EnteredRegion()
	
	workspace.E.BillboardGui.Enabled = true
    print("Player has entered the region")
	
    InputConnection = uis.InputBegan:Connect(function(input, gP) 
		if not gP then
    		if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" then
    			game.Players.LocalPlayer.PlayerGui.TimGui.Frame.Visible = true
			end
    	end
	end)
end


function NpcRange.LeftRegion()
    workspace.E.BillboardGui.Enabled = false
    game.Players.LocalPlayer.PlayerGui.TimGui.Frame.Visible = false
    game.Players.LocalPlayer.PlayerGui.TimGui.ScrollingFrame.Visible = false
    print("Player has left the region")
	InputConnection:Disconnect()
end

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