My script is supposed to make the NPC move forward, if enemy detected stop, if enemy doesn’t exist, move foward again.
EDIT 1:
llocal initialtouched = false
local hitbox = script.Parent.CollisionBox
local moveForward = script.Parent.Movement
hitbox.Touched:Connect(function(hit)
if hit.Name == "CollisionBox" and not initialtouched then
initialtouched = true
moveForward.Disabled = true
print('Disabled')
end
if hit.Name ~= "CollisionBox" then
moveForward.Disabled = false
end
end)
You don’t need to surround an event listener with a while loop. Every 2 seconds you’re adding a new event listener, so when the hitbox gets touched that code is firing dozens if not hundreds of times.
All I see is what you want to do and the code you have, but what is happening instead?
Whoops. I see. You could use @Vaeb 's TouchMod library to get notifications every frame, so the frame you stop recieving notifications from any CollisionBoxes, it resets and you start moving the NPC again. (By frame I mean physics Stepped.)
It’s on Pastebin, but I have a ModuleScript version. Let me know if you want it.
If you’re wondering, no you cannot rely on TouchEnded. It doesn’t work.