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

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

Asked by 5 years ago

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:

01local uis = game:GetService("UserInputService")
02local RegionDetector = require(script.RegionDetector)
03 
04local NpcRange = RegionDetector:NewRegion(workspace.Tim.HumanoidRootPart.Position, 20)
05 
06function NpcRange.EnteredRegion()
07 
08workspace.E.BillboardGui.Enabled = true
09 
10    print("Player has entered the region")
11    local InputConnection
12        uis.InputBegan:Connect(function(input, gP) if not gP then
13    if input.UserInputType.Name == "Keyboard" and
14         input.KeyCode.Name == "E" then
15    game.Players.LocalPlayer.PlayerGui.TimGui.Frame.Visible = true
View all 33 lines...

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

Log in to vote
1
Answered by 5 years ago

You forgot to disconnect the InputConnection

01local uis = game:GetService("UserInputService")
02local RegionDetector = require(script.RegionDetector)
03 
04local InputConnection
05 
06local NpcRange = RegionDetector:NewRegion(workspace.Tim.HumanoidRootPart.Position, 20)
07 
08function NpcRange.EnteredRegion()
09 
10    workspace.E.BillboardGui.Enabled = true
11    print("Player has entered the region")
12 
13    InputConnection = uis.InputBegan:Connect(function(input, gP)
14        if not gP then
15            if input.UserInputType.Name == "Keyboard" and input.KeyCode.Name == "E" then
View all 29 lines...
Ad

Answer this question