Hello everyone. I recently have been making pathfinding zombies and they work amazingly well. They chase and move well. But when I started testing them just now they all of a sudden stopped working. I did not make any changes to the zombies scripts. I think that this could possibly be a bug because they worked perfectly then all of a sudden want to stop working. They spawn and don’t even move. Thanks for any help.
Can I see the script? I can’t help you if I cant see the code.
local pathfindingservice = game:GetService("PathfindingService")
local lowertorso = script.Parent:WaitForChild("LowerTorso")
local uppertorso = script.Parent:WaitForChild("UpperTorso")
local runservice = game:GetService("RunService")
local human = script.Parent:WaitForChild("Humanoid")
local rootpart = script.Parent:WaitForChild("HumanoidRootPart")
local attackSound = script.Parent.Head:WaitForChild("Attack1")
local attack = script.Parent:WaitForChild("Animation")
local attackanim = human:LoadAnimation(attack)
attackanim.Priority = Enum.AnimationPriority.Action --action has the highest proiority
local distance = 500
local ignorelist = {}
if script.Parent.HumanoidRootPart:CanSetNetworkOwnership() then
script.Parent.HumanoidRootPart:SetNetworkOwner(nil)
end
function walkRandomly() --this function makes the npc randomly move when no one is around
local xrand = math.random(-100, 100) --this makes it so it picks a random point between -100 studs and 100 studs and makes it move there
local zrand = math.random(-100, 100)
local goal = rootpart.Position + Vector3.new(xrand, 0, zrand) --adds the random locations together so you can use it for pathfinding
local path = pathfindingservice:CreatePath() --pathfinding stuff
path:ComputeAsync(rootpart.Position, goal)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
human:ChangeState(Enum.HumanoidStateType.Jumping)
end
human:MoveTo(waypoint.Position)
local timeout = human.MoveToFinished:Wait(1)
if not timeout then --if the movetofinished did not wait or work then call the following unstuck function
print("Got Stuck")
human:ChangeState(Enum.HumanoidStateType.Jumping)
if human.Health < 1 then
break
end
main()
end
end
else
print("Path Failed")
wait(1)
walkRandomly() --if it failed then recall the walkrandomly
end
end
function findpath(target)--this function makes the path which the npc follows
local path = game:GetService("PathfindingService"):CreatePath() --determing the stuff for the path the npc will follow
path:ComputeAsync(rootpart.Position,target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
human:ChangeState(Enum.HumanoidStateType.Jumping)
end
human:MoveTo(waypoint.Position)
path.Blocked:Connect(function()
findpath(target) --instead of jumping when the path is blocked it is best to recall the findpath function
end)
local timeout = human.MoveToFinished:Wait(1)
if not timeout then --same as above if it does not do the movetofinished and wait then it redoes the findpath functiona and breaks this function
human:ChangeState(Enum.HumanoidStateType.Jumping)
findpath(target)
print("Took Too Long")
break
end
if checkSight(target) then --if the npc sees the target which is defined by a function below
repeat
human:MoveTo(target.Position) --moving the npc to the target
attackTarget(target) --attacks the npc using the attack function
wait(0.1)
if target == nil then
break --all of this checks if the target is true and if not then breaks the repeat loop of the npc moving
elseif target.Parent == nil then
break
end
until checkSight(target) == false or human.Health < 1 or target.Parent.Humanoid.Health < 1 --stops the repeat function if the target dies or if the npc dies
break
end
if (rootpart.Position - waypoints[1].Position).magnitude > 30 then --if the rootpart - the last waypoint distance is greater than 10 (Besically if the target is far away and moves away then it reloads the path)
findpath(target)
break
end
end
end
end
function checkSight(target) --This function uses raycasting to check the distance and hieght of the enemy
local ray = Ray.new(rootpart.Position,(target.Position - rootpart.Position).Unit * 150 )--starts at the root part then shoot to the target from the rootpart and chooses the direction
table.insert(ignorelist, script.Parent)
local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, ignorelist) --ignores all parts on the script.Parent
if hit then --if it hit something
if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - rootpart.Position.Y) < 10 then --sees if it relates to the target then checks to see if the hieght is similar to the npcs hieght
return true
end
end
return false
end
function findTarget() --this function searches for the target
local distance = 500
local target = nil
local potentialtargets = {}
local seeTargets = {}
for i, v in ipairs(game.Workspace:GetChildren()) do
local humanoid = v:FindFirstChild("Humanoid")
local humanoidRootPart = v:FindFirstChild("HumanoidRootPart")
if humanoid and humanoidRootPart and v.Name ~= script.Parent.Name and v.Name ~= "ArmyZombie" and v.Name ~= "BossZombie" and v.Name ~= "BuffZombie" and v.Name ~= "Demon" and v.Name ~= "DoctorZombie" and v.Name ~= "GoldZombie" and v.Name ~= "PoliceZombie" and v.Name ~= "ReaperZombie" and v.Name ~= "RobberZombie" and v.Name ~= "SpeedZombie" and v.Name ~= "SwiperZombie" and v.Name ~= "Zombie" and humanoid.Health > 0 then
if (rootpart.Position - humanoidRootPart.Position).magnitude < distance then
table.insert(potentialtargets, humanoidRootPart) --adds the target that we found into the potential targets table so we can make sure its within range and is a good target
end
end
end
if #potentialtargets > 0 then --if the number of potential targets is greater than 0
for i, v in ipairs(potentialtargets) do --loops through potential targets
if checkSight(v) then -- runs the checksight function and sends the potential target to check
table.insert(seeTargets, v) --inserts the target into the seetargets
elseif #seeTargets == 0 and (rootpart.Position - v.Position).magnitude < distance then --if the number if targets seen is equal to 0 and if its not within distance then
target = v
distance = (rootpart.Position - v.Position).magnitude
end
end
end
if #seeTargets > 0 then --if number of targets seen is greater than 0 then
distance = 500
for i, v in ipairs(seeTargets) do
if (rootpart.Position - v.Position).magnitude < distance then
target = v
distance = (rootpart.Position - v.Position).magnitude
end
end
end
if target then
if math.random(20) == 1 then
script.Parent.Head.Sound1:Play()
end
end
return target
end
function attackTarget(target) --this function plays the attack function as well as damages
if (rootpart.Position - target.Position).magnitude < 2 then
attackanim:Play()
attackSound:Play()
if target.Parent ~= nil then
target.Parent.Humanoid:TakeDamage(20)
end
wait(0.4)
end
end
local debounce = false
lowertorso.Touched:Connect(function(obj)--this function is if pathfinding messes up
if debounce == false then
debounce = true
if not obj.Parent:FindFirstChild("Humanoid") then
human:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
wait(1)
debounce = false
end)
human.Died:Connect(function()
script:Destroy()
end)
function main() --this is the function that checks to make sure if its a target and if so then starts the path and if not then makes it walk randomly
local target = findTarget()
if target then
human.WalkSpeed = 22
findpath(target)
else
human.WalkSpeed = 12
walkRandomly()
end
end
while wait(0.01) do --this calls the main function
if human.Health < 1 then
break
end
main()
end
Is there some wall near them??
No there is a small block that is in the air that they spawn at and they spawn and fall and normally chase and what not but all of a sudden they decided not to work and not chase
I had the same problem with this earlier but it fixed itself somehow but now it is doing it again and I dont know what to do to stop it and fix it
Are there any error codes in the console?
No the only thing that goes in the output is once I die it says path failed in the walk randomly function