The script below is a child of an npc. Basically I wanted to make it so that when the npc is touched by a player that it will stop moving to random parts. But when I found out that my script didn't work I just wanted to check if the npc would detected being touched in general and that didn't even go through.
local pathdeterminer local npc = script.Parent local humanoid = npc:WaitForChild("Humanoid") local inbattle = false while inbattle == false do pathdeterminer = "p" .. math.random(1,6) if(pathdeterminer == "p1") then humanoid:MoveTo(game.Workspace.Points.p1.Position) humanoid.MoveToFinished:Wait() end if(pathdeterminer == "p2") then humanoid:MoveTo(game.Workspace.Points.p2.Position) humanoid.MoveToFinished:Wait() end if(pathdeterminer == "p3") then humanoid:MoveTo(game.Workspace.Points.p3.Position) humanoid.MoveToFinished:Wait() end if(pathdeterminer == "p4") then humanoid:MoveTo(game.Workspace.Points.p4.Position) humanoid.MoveToFinished:Wait() end if(pathdeterminer == "p5") then humanoid:MoveTo(game.Workspace.Points.p3.Position) humanoid.MoveToFinished:Wait() end if(pathdeterminer == "p6") then humanoid:MoveTo(game.Workspace.Points.p4.Position) humanoid.MoveToFinished:Wait() end wait(0.5) end humanoid.Touched:Connect(function(hit) local check = hit.Parent:WaitForChild("Humanoid") if check ~= nil then print("TOUCHED") inbattle = true end end)
Remember that when using a loop, the loop will yield the current thread until it either finishes or it's condition is no longer met.
You can notice this here, as the code below will not run due to an infinite loop:
while true do print("i am infinitely running!") wait(1) end print("i'll never get to run because of that infinite loop...")
You can solve this by putting the humanoid.Touched
portion of your code above the loop. Remember that you should keep infinite loops to a minimum when possible, and always place them at the bottom.