You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
Ai change from walking to running after see player -
What is the issue? Include screenshots / videos if possible!
Ai keep playing animation of running instead of going back to walking after lost sight of player
( run to last player’s position).

-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
–
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local PathfindingService = game:GetService("PathfindingService")
local Runservice = game:GetService("RunService")
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local hrp = npc:WaitForChild("HumanoidRootPart")
hrp:SetNetworkOwner(nil)
local walkAnim = humanoid.Animator:LoadAnimation(script.Walk)
local attackAnim = humanoid.Animator:LoadAnimation(script.Attack)
local RunAnim = humanoid.Animator:LoadAnimation(script.Run)
local pathParams = {
AgentHeight = 6.5,
AgentRadius = 6,
AgentCanJump = true,
}
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {npc}
local lastPos
local animPlaying = false
local RANGE = 60
local Speed = 60 --60
local NorSpeed = 16 --16
local function canSeeTarget(target)
local orgin = hrp.Position
local direction = (target.HumanoidRootPart.Position - hrp.Position).Unit * RANGE
local ray = workspace:Raycast(orgin, direction, rayParams)
if ray and ray.Instance then
if ray.Instance:IsDescendantOf(target) then
return true
else
return false
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = RANGE
local nearestTarget
for i, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
nearestTarget = target
maxDistance = distance
humanoid.WalkSpeed = Speed
RunAnim:Play()
end
end
end
return nearestTarget
end
local function getPath(destination)
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(hrp.Position, destination.Position)
return path
end
local function attack(target)
local distance = (hrp.Position - target.HumanoidRootPart.Position).Magnitude
local debounce = false
if distance > 5 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
if debounce == false then
debounce = true
npc.Head.AttackSound:Play()
attackAnim:Play()
target.Humanoid.Health = 0
task.wait(0.5)
debounce = false
end
end
end
local function walkTo(destination)
local path = getPath(destination)
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(path:GetWaypoints()) do
path.Blocked:Connect(function()
path:Destroy()
end)
if animPlaying == false then
walkAnim:Play()
animPlaying = true
end
attackAnim:Stop()
local target = findTarget()
if target and target.Humanoid.Health > 0 then
lastPos = target.HumanoidRootPart.Position
attack(target)
break
else
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
if lastPos then
humanoid:MoveTo(lastPos)
humanoid.MoveToFinished:Wait()
lastPos = nil
humanoid.WalkSpeed = NorSpeed
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
humanoid.WalkSpeed = NorSpeed
end
end
end
else
return
end
end
local function patrol()
local waypoints = workspace.Waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end
while Runservice.Heartbeat:Wait(0.2) do
patrol()
end
Context: I have a little bit of knowledge of scripting, after some manipulating script, It seem working!
but the problem start happening after adding animation. I’m really sorry, this is my first post.