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

NPC continues to teleport after told not to?

Asked by 4 years ago

I am trying to make it so that when the NPC is touched once by a humanoid it won't check for being touched anymore, But when test this the NPC continues to follow the humanoid.Touched function even though it should not.

local pathdeterminer
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local inbattle = false
local enemyarea = game.Workspace.BattleCircle:WaitForChild("Enemies")

if inbattle == false then
humanoid.Touched:Connect(function(hit)
    local check = hit.Parent:WaitForChild("Humanoid")
    if check ~= nil then
        inbattle = true
        npc.Torso.CFrame = enemyarea:WaitForChild("E1").CFrame + Vector3.new(0,3,0)
    end 
end)
end

1 answer

Log in to vote
0
Answered by
Norbunny 555 Moderation Voter
4 years ago

In order to stop listening to an event, you need to disconnect from it! You can do so using :Disconnect().

Create a function that will be executed when the event is fired, and create a variable for the connection. It is simpler than it sounds!

You can find Roblox's documentation on :Disconnect() here!

Applying it to your code would be something like:

local function onTouch()
    -- function ran when the humanoid is touched

    -- Do your checks and then
    connection:Disconnect()
    -- The humanoid will no longer listen to the touch event
end

local connection = humanoid.Touched:Connect(onTouch)

Ad

Answer this question