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

NPC continues to teleport after told not to?

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

✓ Accepted answer

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)

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