Hi, I made a dummy that is supposed to follow the player for 3 seconds and, after the 3 seconds, go straight back to a part called "p1", but the NPC does not go back to the part but just stops. Can anyone help me?
``
Script: local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local plrsHit = {}
local maxDistance = math.huge
local chaseDuration = 3
local p1 = workspace:FindFirstChild("p1")
npc.Humanoid.Touched:Connect(function(touch)
local player = game.Players:GetPlayerFromCharacter(touch.Parent)
if player and npc.Humanoid.Health > 0 and not plrsHit[player] then
local playerCharacter = player.Character
if playerCharacter and playerCharacter:FindFirstChild("Humanoid") and playerCharacter.Humanoid.Health > 0 then
plrsHit[player] = true
playerCharacter.Humanoid:TakeDamage(20)
wait(1)
plrsHit[player] = false
end
end
end)
while wait() do
local plrs = game.Players:GetPlayers()
local closestHRP
for i, plr in pairs(plrs) do
local playerCharacter = plr.Character
if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") and playerCharacter.Humanoid.Health > 0 then
local hrp = playerCharacter.HumanoidRootPart
local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude
if not closestHRP then
closestHRP = hrp
end
if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
closestHRP = hrp
end
end
end
if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
npc.Humanoid:MoveTo(closestHRP.Position)
end
if npc.Humanoid.Health <= 0 then
npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
for _, player in pairs(plrs) do
plrsHit[player] = false
end
npc.Humanoid.Health = 0
break
end
if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
wait(chaseDuration)
if p1 then
npc.Humanoid:MoveTo(p1.Position)
else
warn("Part 'p1'")
end
end
end``
** You are now Level 1! **