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

How can i stop listening for inputs after a player has exited an area?

I have a script in local scripts that checks a few things; and then the script start listening for E as a player imput. It works as defined but i overlooked that even after the player walks away from the door and the sensor they can still open it

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
function SendDoorOpenRequest(RemoteEvent)
    RemoteEvent:FireServer()
end
wait(2)
P = game.Players.LocalPlayer
AccessLevel = P.AccessLevel
local DOL = script.Parent.Parent.PlayerGui.GameGUIS.DoorOpenLabel
function DOLTT(T)
    local goal = {}
    goal.TextTransparency = (T)
    local TTween = TweenService:Create(DOL,TweenInfo.new(1),goal)
    TTween:Play()
end
P.Character.HumanoidRootPart.TouchEnded:Connect(function(Touch)
    DOLTT(1)
    
end)
P.Character.HumanoidRootPart.Touched:Connect(function(Touch)
    if Touch.Parent.DoorOpeningEvent then
        DOLTT(0)
        local Event = Touch.Parent.DoorOpeningEvent
        P. Touch.TouchingUserInputService.InputBegan:Connect(function(Input)
            if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Enum.KeyCode.E then
                if AccessLevel.Value >= Touch.Parent.AccessLevelRequired.Value then
                    SendDoorOpenRequest(Event)
                    DOLTT(1)
                else
                    DOLTT(1)
                    wait(1)
                    P.PlayerGui.GameGUIS.DoorOpenLabel.Text = ("You do not have the proper access levels to open this door.")
                    DOLTT(0)
                    wait(2)
                    DOLTT(1)
                    wait(1)
                    P.PlayerGui.GameGUIS.DoorOpenLabel.Text = ("Press E to open door.")
                    DOLTT(0)
                end
            end
        end)
    end
end)

is there a way i can make it detect the player walking away without massively overcomplicating things?

2 comments

0

You can do a check to make sure the player is close enough. if (player.Character.HumanoidRootPart.Position - door.Position).Magnitude >= 15 then

0

Basically if players humanoidrootpart is within 15 studs of the doors position, run code

1 answer

As WizyTheNinja said, you can do a check to make sure the player is close enough.

 if (player.Character.HumanoidRootPart.Position - door.Position).Magnitude >= 15 then

An honerable mention of BasePart:GetTouchingParts, by the way.

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