I made a script that checks if players are nearby, and which one is closest. Then it sees if the player is close enough to attack. If not, then it sees if the player is within line of sight of the zombie and if it is, it walks straight there, but if it isn't, it uses path finding service to get there
My problem is that occasionally, the zombie will just stand still for a few seconds. I believe that the issue happens when the nearest player goes in/out of the line of sight. Any ideas as to why this could be happening? Thank you for your time! This is the section which I believe is giving me the issue:
--Do Zombie Stuff
local function DoZombStuff()
NearestPlr=nil
NearestDist=1000
NearestLoc=nil
--Check if players are nearby/Closest player
for i,v in pairs(game.Players:getChildren()) do
if v.Character~=nil and v.Character:findFirstChild("HumanoidRootPart") and v.Character:findFirstChild("Humanoid") and v.Character.Humanoid.Health>0 then
if (v.Character.HumanoidRootPart.Position-HRP.Position).magnitude<NearestDist then
NearestPlr=v.Character
NearestDist=(v.Character.HumanoidRootPart.Position-HRP.Position).magnitude
NearestLoc=NearestPlr.HumanoidRootPart.Position
end
end
end
--Create ray
if NearestPlr~=nil then
local Hit
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {script.Parent.Parent,workspace:WaitForChild("Explosions")}
raycastParams.IgnoreWater = true
local Ray=workspace:Raycast(HRP.Position, (NearestPlr.HumanoidRootPart.Position-HRP.Position).Unit*200, raycastParams)
if Ray and Ray.Instance then
Hit=Ray.Instance
end
--Attack
if (NearestLoc-HRP.Position).magnitude<=AttDist+1 and ADB==false and Humanoid.Health>0 then
ADB=true
Humanoid:MoveTo(HRP.Position,NearestPlr.HumanoidRootPart)
wait(.1)
HRP.CFrame=CFrame.new(HRP.Position,Vector3.new(NearestLoc.X,HRP.Position.Y,NearestLoc.Z))
if NearestPlr:findFirstChild("Humanoid") then
NearestPlr.Humanoid.Health=NearestPlr.Humanoid.Health-dmg
end
ThrowAnimationTrack:Play()
wait(AttackCoolDown)
ADB=false
--In LoS
elseif Hit~=nil and (Hit.Parent==NearestPlr or Hit.Parent.Parent==NearestPlr) and (NearestLoc-HRP.Position).magnitude>=AttDist then
Humanoid:MoveTo(NearestLoc)
--Pathfind
elseif (NearestLoc-HRP.Position).magnitude<=1000 and (NearestLoc-HRP.Position).magnitude>=AttDist then
local path=game:GetService("PathfindingService"):findPathAsync(HRP.Position,NearestPlr.HumanoidRootPart.Position)
local points=path:GetWaypoints()
--Walk to target
for i,v in pairs(points) do
--Blocked
local isBlocked=false
path.Blocked:Connect(function()
isBlocked=true
end)
if isBlocked==true then
return
end
--Jump
if v.Action==Enum.PathWaypointAction.Jump then
Humanoid.Jump=true
end
--Move to Next position
Humanoid:MoveTo(v.Position)
if NearestPlr~=nil and NearestPlr:findFirstChild("HumanoidRootPart") and (points[#points-1].Position-NearestPlr.HumanoidRootPart.Position).magnitude>=4 then
if points[i+1] then
Humanoid:MoveTo(points[i+1].Position)
else
Humanoid:MoveTo(NearestLoc)
end
return
end
Humanoid.MoveToFinished:Wait()
end
end
end
end
while wait() do
DoZombStuff()
end
