Please help me, I want to create a pathfinding npc that attacks and I want to make it so whenever a player is touching a block it will follow him but if he isn't it won't
The basic script is
local sp = script.Parent
local newCharacter = sp:Clone()
newCharacter.Parent = game.ServerStorage
wait(0.5)
pathFind = game:GetService("PathfindingService")
local humanoid = sp:WaitForChild("Humanoid")
local torso = sp:WaitForChild("Torso")
local target = sp:WaitForChild("Target")
local location = sp:WaitForChild("Location")
local sp = script.Parent
local humanoid = sp:WaitForChild("Humanoid")
local toolNone = script:FindFirstChild("toolnone")
local toolNoneAnim = toolNone:FindFirstChild("ToolNoneAnim")
local a = Vector3.new(100,5,-100)
local b = Vector3.new(75,5,-200)
local debounce = true
local debounce2 = false
local toolNoning = humanoid:LoadAnimation(toolNoneAnim)
local list = game.Players:GetChildren()
local loco = 100000
sp.PrimaryPart = sp:WaitForChild("Head")
if target.Value then
local targetTorso = target.Value:FindFirstChild("Torso")
end
function attack(hit)
if hit and hit.Parent then
local enemy = hit.Parent:FindFirstChild("Humanoid")
if enemy then
if enemy:IsA("Humanoid") and hit.Parent.Name ~= sp.Name then
debounce = true
while debounce do
toolNoning:Play()
wait(1)
torso.TouchEnded:connect(function(hit2)
if hit2 == hit then
debounce = false
end
end)
end
end
else
if hit.CanCollide then
humanoid.Jump = true
end
end
end
end
function findPlayers()
list = game.Players:GetChildren()
for i=1,#list do
if list[i] and list[i]:IsA("Player") then
local found = list[i].Character:FindFirstChild("Humanoid")
if found and found.Health>0 then
local loc = (torso.Position - list[i].Character.Torso.Position).magnitude
if loc < loco then
loco = loc
target.Value = list[i].Character
end
end
end
end
end
while humanoid.Health > 0 do
target = sp:WaitForChild("Target")
location = sp:WaitForChild("Location")
findPlayers()
if target.Value then
targetTorso = target.Value:FindFirstChild("Torso")
if targetTorso then
path = pathFind:ComputeRawPathAsync(torso.Position,targetTorso.Position,500)
points = path:GetPointCoordinates()
if #points <10 or #points == 10 then
mx = #points
l = true
else
mx = 10
l = false
end
for i = 1,mx/2 do
wait(0.25)
humanoid:MoveTo(points[i*2])
if i == mx/2 and l then
humanoid:MoveTo(targetTorso.Position,targetTorso)
end
end
end
else
path = pathFind:ComputeRawPathAsync(torso.Position,location.Value,500)
points = path:GetPointCoordinates()
if #points <10 then
mx = #points
else
mx = 10
end
for i = 1,mx do
humanoid:MoveTo(points[i])
wait(0.25)
end
humanoid:MoveTo(location.Value)
end
loco = 100000
torso.Touched:connect(attack)
wait(0.5)
end
if humanoid.Health == 0 or humanoid.Health < 0 then
wait(5)
newCharacter.Parent = Workspace
wait(0.1)
sp:Destroy()
end
