i watch @GnomeCode tutorial and he’s ai pathfinding isnt working good
the problem was when i go to the wall this happen
here’s the code
local Zombie = script.Parent
local humanoid = Zombie.Humanoid
Zombie.PrimaryPart:SetNetworkOwner(nil)
local function canSeeTarget(target)
local origin = Zombie.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - Zombie.HumanoidRootPart.Position).unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, Zombie)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 40
local nearestTarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (Zombie.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local PathfindingService = game:GetService("PathfindingService")
local pathParams = {
["AgentHeight"] = 14,
["AgentRadius"] = 5,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(Zombie.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
local distance = (Zombie.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
local jumpscareevent = game.ReplicatedStorage.Event.ZombieJumpScare
local plr = game.Players:GetPlayerFromCharacter(target)
if distance > 8 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local jumpscare = humanoid.Animator:LoadAnimation(script:WaitForChild("JumpScare"))
jumpscare:Play()
target.Head.CFrame = Zombie.LeftHand.CFrame
humanoid.WalkSpeed = 0
target.HumanoidRootPart.CFrame = Zombie.HumanoidRootPart.CFrame * CFrame.new(0,0,-3)
target.HumanoidRootPart.Anchored = true
target.Humanoid.WalkSpeed = 0
jumpscareevent:FireClient(plr, Zombie)
jumpscare.Stopped:Wait()
humanoid.WalkSpeed = 16
target.Humanoid.Health = 0
end
end
function walkTo(destination)
print(tonumber(destination.Position - (Zombie.HumanoidRootPart.CFrame.LookVector * 10)))
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for i, v in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
attack(target)
break
else
humanoid:MoveTo(v.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(destination.Position - (Zombie.HumanoidRootPart.CFrame.LookVector * 10))
end
end
function patrol()
local waypoints = workspace.Waypoint:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while true do
wait(.1)
patrol()
end
there is no error in the code just the ai isnt working good