Hello,
Can someone help me to improve my follow Script, because its still do not work how I want to
At the Moment:
The Zombie first go to the BaseBlock (Pink Block) and if the player is attack range of the zombie
he ignores the player and still go toward the BaseBlock. After the Zombie reach the block and if the player is in attack range he goes to the player.
My Goal:
The Zombie knows what is wall and can go around it - That’s Already done!
The Zombie is going to the nearest one however it is a BaseBlock or Humanoid and do not focus on one target but change the target whoever is the nearest one
Follow Script
local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
local target
local waypoints
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = _M.FollowDistance
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while wait(0.1) do
target = findNearestTorso(script.Parent.HumanoidRootPart.CFrame.p)
if target ~= nil and (target.CFrame.p - script.Parent.HumanoidRootPart.CFrame.p).magnitude > 4 then
path:ComputeAsync(Mob.HumanoidRootPart.Position,target.CFrame.p)
waypoints = path:GetWaypoints()
for i, waypoint in pairs (waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
Enemy:ChangeState(Enum.HumanoidStateType.Jumping)
end
Enemy:MoveTo(waypoint.Position)
Enemy.MoveToFinished:Wait(0.1)
end
else
path:ComputeAsync(Mob.HumanoidRootPart.Position,workspace.DefendingPoint.Position)
waypoints = path:GetWaypoints()
for i, waypoint in pairs (waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
Enemy:ChangeState(Enum.HumanoidStateType.Jumping)
end
Enemy:MoveTo(waypoint.Position)
Enemy.MoveToFinished:Wait(0.1)
end
end
end
Screen: