wait this is a script i made that has both a pathfinding system and a animation system but it doesnt work: local entity = script.Parent
local humanoid = entity.Humanoid
local PathfindingService = game:GetService("PathfindingService")
entity.PrimaryPart:SetNetworkOwner(nil)
-- Load animations
local walkAnim = humanoid:LoadAnimation(script.Walk) -- Walk animation
local runAnim = humanoid:LoadAnimation(script.Run) -- Run animation
local defaultWalkSpeed = 6 -- Speed for patrolling
local chaseSpeed = 18 -- Speed for chasing targets
local currentState = "Idle" -- Track movement state ('Idle', 'Walking', 'Running')
local currentTarget = nil -- Keep track of the current target
local function setAnimationState(newState)
if currentState == newState then return end -- Avoid unnecessary state changes
-- Stop all animations before switching
walkAnim:Stop()
runAnim:Stop()
if newState == "Walking" then
walkAnim:Play()
humanoid.WalkSpeed = defaultWalkSpeed
elseif newState == "Running" then
runAnim:Play()
humanoid.WalkSpeed = chaseSpeed
end
currentState = newState
end
local function canSeeTarget(target)
local origin = entity.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - origin).Unit * 40
local ray = Ray.new(origin, direction)
local hit, _ = workspace:FindPartOnRay(ray, entity)
return hit and hit:IsDescendantOf(target) -- Returns true if target is visible
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 200
local nearestTarget = nil
for _, player in pairs(players) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local target = player.Character
local isHiding = target:FindFirstChild("Actions") and target.Actions:FindFirstChild("IsHiding") and target.Actions.IsHiding.Value
local distance = (entity.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) and not isHiding then
nearestTarget = target
maxDistance = distance
end
end
end
return nearestTarget
end
local function getPath(destination)
local pathParams = {
["AgentHeight"] = 12.25,
["AgentRadius"] = 4.2,
["AgentCanJump"] = false
}
local path = PathfindingService:CreatePath(pathParams)
path:ComputeAsync(entity.HumanoidRootPart.Position, destination.Position)
return path
end
local function attack(target)
if target.Humanoid.Health <= 0 then return end -- Stop if the target is already dead
local distance = (entity.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance > 5 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
setAnimationState("Idle") -- Stop movement animations before attacking
local attackAnim = humanoid:LoadAnimation(script.Attack)
attackAnim:Play()
target.Humanoid.Health = 0 -- Kill the target
end