-
What do you want to achieve?
Smooth Movement without any interruption. -
What is the issue? Include screenshots / videos if possible!
Video:
The AI keeps flickering the chracter to the left, which i believe is because it thinks there is no player so it trys to patrol again.
Script:
local Players = game:GetService("Players") local PathfindingService = game:GetService("PathfindingService") local zombieDamage = 100 --attack damage dealed to player local zombieAttackCd = 3 --cooldown time before next attack local db = false local rig = script.Parent local function checkForCharacter(character) local rayOrigin = rig:FindFirstChild("HumanoidRootPart").Position local rayDirection = (character.HumanoidRootPart.Position - rayOrigin).Unit * 40 local raycastResult = workspace:Raycast(rayOrigin, rayDirection, RaycastParams.new()) if raycastResult then local raycastInstance = raycastResult.Instance if raycastInstance:IsDescendantOf(character) then return true end else return false end end local function findNearestPlayer() local players = Players:GetPlayers() local nearestPlayer = nil local maxDistance = 500 for _, player in pairs(players) do if player.Character ~= nil then local targetCharacter = player.Character local distance = (rig.HumanoidRootPart.Position - targetCharacter.HumanoidRootPart.Position).Magnitude if distance < maxDistance and checkForCharacter(targetCharacter) then nearestPlayer = targetCharacter maxDistance = distance end end end return nearestPlayer end local function attack(character) local cdTime = zombieAttackCd local distance = (rig.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude if distance > 2.5 then rig.Humanoid:MoveTo(character.HumanoidRootPart.Position) else if db == false then character.Humanoid.Health -= zombieDamage end db = true task.defer(function() task.wait(cdTime) db = false end) end end local function calculatePath(destination) local agentParams = { ["AgentHeight"] = 5, ["AgentRadius"] = 4, ["AgentCanJump"] = false } local path = PathfindingService:CreatePath(agentParams) path:ComputeAsync(rig.HumanoidRootPart.Position, destination) return path end local function walkToDestination(destination) local path = calculatePath(destination) if path.Status == Enum.PathStatus.Success then for _, waypoint in pairs(path:GetWaypoints()) do local nearestPlayer = findNearestPlayer() if nearestPlayer then attack(nearestPlayer) break else rig.Humanoid:MoveTo(waypoint.Position) rig.Humanoid.MoveToFinished:Wait() end end else rig.Humanoid:MoveTo(destination - (rig.HumanoidRootPart.CFrame.LookVector * 10)) end end local function patrol() local waypoints = workspace.Waypoints:GetChildren() local randomNumber = math.random(1, #waypoints) walkToDestination(waypoints[randomNumber].Position) end while task.wait(0.01) do patrol() if rig.Humanoid.Health == 0 then rig:Destroy() end end Thanks for the Help! ![]()