Hello, I’m trying to get this enemy to path find their way towards you if you get close enough but it doesn’t seem to move. How do I solve this?
--
local hum = script.Parent:WaitForChild("Humanoid")
local root = script.Parent:WaitForChild("HumanoidRootPart")
local spawnPos = script.Parent:WaitForChild("SpawnPosition")
local visionRange = 30
local attackRange = 3
local abandonRange = 40
local abandonRangeFromHome = 200
local target = nil
while wait() do
if target then
local plrRoot = target.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitude
local distancefromhome = (spawnPos.Value - plrRoot.Position).magnitude
local PathfindingService = game:GetService("PathfindingService")
local calculation = hum.WalkSpeed/3.675
local TimePerStep = 1/calculation
local HumanoidProperties = {
["AgentHeight"] = 5,
["AgentRadius"] = visionRange,
["AgentCanJump"] = true
}
--Makes Path
local Path = PathfindingService:CreatePath(HumanoidProperties)
--Makes Humanoid Follow Waypoints to a target
function GoTo(Target)
--Computes Path and Waypoints
Path:ComputeAsync(root.Position, Target.Position)
if Path.Status == Enum.PathStatus.NoPath then
else
local WayPoints = Path:GetWaypoints()
--Makes humanoid follow the Waypoints
for _, WayPoint in ipairs(WayPoints) do
hum:MoveTo(plrRoot.Position - CFrame.new(root.Position, plrRoot.Position).LookVector * attackRange)
wait(TimePerStep)
if WayPoint.Action == Enum.PathWaypointAction.Jump then
hum.Jump = true
end
end
end
end
while wait(0.25) do
GoTo(target)
end
if distance <= attackRange + 2 then
script.AttackRemote:Fire(plrRoot)
end
if distance > abandonRange then
target = nil
hum:MoveTo(spawnPos.Value)
end
if distancefromhome > abandonRangeFromHome then
target = nil
hum:MoveTo(spawnPos.Value)
end
else
for i,v in pairs(game.Players:GetChildren()) do
if not game.Workspace:FindFirstChild(v.Name) then continue end
local char = game.Workspace[v.Name]
local plrRoot = char.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitude
local distancefromhome = (spawnPos.Value - plrRoot.Position).magnitude
if distance < visionRange and distancefromhome < abandonRangeFromHome then
target = char
end
end
end
end