Why won’t this script detect and chase the player?
local PathFindingService = game:GetService("PathfindingService")
local WorkSpace = game:GetService("Workspace")
local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local myBody = script.Parent:WaitForChild("Body")
local head = script.Parent.Body:WaitForChild("Head")
local lowerTorso = script.Parent.Body:WaitForChild("LowerTorso")
myRoot:SetNetworkOwner(nil)
myBody:SetNetworkOwner(nil)
local pathArg = {
AgentHeight = 11,
AgentRaidus = 3
}
function wanderRandomly()
local randX = math.random(0, 250)
local randZ = math.random(0, 500)
local goal = myRoot.Position + Vector3.new(randX, 0, randZ)
local path = PathFindingService:CreatePath(pathArg)
path:ComputeAsync(myRoot.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, waypoint in ipairs(waypoints) do
myHuman:MoveTo(waypoint.Position)
myHuman.MoveToFinished:Wait(3)
end
end
end
function findTarget()
for _, human in pairs(WorkSpace:GetChildren()) do
if human:FindFirstChild("Humanoid") then
if not human:FindFirstChild("NPC") then
local humT = human:FindFirstChild("Humanoid")
local humRP = human:FindFirstChild("HumanoidRootPart")
local distance = (humRP.Position - myRoot.Position).magnitude
print(distance)
if distance < 7 then
return human
end
end
end
end
end
function chasePlayer(target)
local path = PathFindingService:CreatePath(pathArg)
path:ComputeAsync(myRoot.Position, target.HumanoidRootPart.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, waypoint in ipairs(waypoints) do
myHuman:MoveTo(waypoint.Position)
myHuman.MoveToFinished:Wait()
end
end
end
while task.wait() do
local target = findTarget()
if target then
print(target)
chasePlayer(target)
myHuman.WalkSpeed = 18
else
wanderRandomly()
myHuman.WalkSpeed = 9.5
end
end