Animations Lagging And Don't Know Why

i created a pathfind but the animation of the ogre is glitching out!
robloxapp-20240112-2130251.wmv
(i don’t know how to publish a video)

but here’s the code:

local Character = script.Parent
local humanoid = Character.Humanoid
Character.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
	local origin = Character.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).unit * 40
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, Character)
	
	
	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 = (Character.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"] = 8.125,
		["AgentRadius"] = 3,
		["AgentCanJump"] = false
	}
	
	local path = PathfindingService:CreatePath(pathParams)
	
	path:ComputeAsync(Character.HumanoidRootPart.Position, destination.Position)
	
	return path
end

local function attack(target)
	local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 8 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		local attackAnim = humanoid:LoadAnimation(script.Attack)
		attackAnim:Play()
		target.Humanoid.Health = 0
	end
end

local function walkTo(destination)
	
	local path = getPath(destination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				print("TARGET FOUND", target.Name)
				attack(target)
				break
			else
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (Character.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

function patrol()
	local waypoints = workspace.Waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while wait(0.25) do
	patrol()
end
1 Like

That last function there, that while wait(0.25) one, I believe it’s restarting the animation every time it runs, try making the animation a separate function that will detect when to run

1 Like

it can’t be i borrowed the code from my another game

Your right, it’s not. Perhaps check and make sure the ogre is rigged properly?

1 Like

it’s rigged correctly but i watched a tutorial but the thing it says is to use networkOwnership but iv’e used it

1 Like

if you look i coded it so idk what is going on

1 Like