Not Moving Forward After Collision?

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.

It didnt work, I’ve updated it

Is it a localscript?
or a normal script?

This reply is here to help someone solve this.
Why do we need a character limit? :thinking: 30 chars

1 Like

No, the script is NOT a local script.

1 Like

Is it erroring because of the llocal? (It’s local)

No, that was a typo. It is correct in studio

Okay. What exactly is the problem here?

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.

2 Likes