Somethings wrong with my pathfinding component

Somethings wrong with it and its not working
help pls

something in the Start function is not working

Right now the enemies don’t move at all
sending a rbxl down below

local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")

local ComponentModule = require(ServerScriptService.Component)

local Component = ComponentModule:New()

function Component:Start()
	self.Character = self.Instance
	
	self.Root = self.Character.HumanoidRootPart
	self.Humanoid = self.Character.Humanoid
	
	self.Path = PathfindingService:CreatePath({
		AgentCanJump = false,
	})
	
	
	-- ComputationLoop
	task.spawn(function()
		while self.Character:IsDescendantOf(game) and self.Character:FindFirstChild("Humanoid") do
			local ClosestPlayer, ClosestPlayerDistance
			
			for _,Player in pairs(Players:GetPlayers()) do
				if Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
					if not ClosestPlayerDistance then
						ClosestPlayer, ClosestPlayerDistance = Player, (Player.Character.HumanoidRootPart.Position - self.Root.Position).Magnitude
					
					elseif ClosestPlayerDistance > (Player.Character.HumanoidRootPart.Position - self.Root.Position).Magnitude then
						ClosestPlayer, ClosestPlayerDistance = Player, (Player.Character.HumanoidRootPart.Position - self.Root.Position).Magnitude
					end
				end
			end
			
			if ClosestPlayer then
				if ClosestPlayerDistance > self.Character:GetAttribute("AgroDistance") then
					self.Path:ComputeAsync(self.Root.Position, ClosestPlayer.Character.HumanoidRootPart.Position)
					self.Waypoints = self.Path:GetWaypoints()
					self.NextTicket = 1
				else
					self.NextTicket = 1
					self.Waypoints = nil
				end
			else
				self.NextTicket = 1
				self.Waypoints = nil
			end
			
			task.wait(0.1)
		end
	end)
	
	-- MovementLoop
	task.spawn(function()
		while self.Character:IsDescendantOf(game) and self.Character:FindFirstChild("Humanoid") do
			if self.Waypoints and #self.Waypoints ~= 0 then
				self.Humanoid:MoveTo(self.Waypoints[self.NextTicket]["Position"])
			end
			
			task.wait(0.1)
		end
	end)
	
	-- MoveOnFinished
	--self.Humanoid.MoveToFinished:Connect(function()
	--	if self.Waypoints then
	--		self.NextWaypoint += 1
			
	--		if self.Waypoints[self.NextWaypoint] then
	--			self.Humanoid:MoveTo(self.Waypoints[self.NextWaypoint]["Position"])
	--		end
	--	end
	--end)
	
	self.Humanoid.Died:Connect(function()
		self.Humanoid.Health = 100
		
		self.Character:Clone().Parent = workspace
		self.Character:Destroy()
	end)
end

function Component:Stop()
	-- Nothing to clean but function is required :)
end

Component:BindToTag("Zombie")

return Component
1 Like

This may help …

Cleaned it up a bit
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")

local ComponentModule = require(ServerScriptService.Component)
local Component = ComponentModule:New()

function Component:Start()
    self.Character = self.Instance
    self.Root = self.Character:WaitForChild("HumanoidRootPart")
    self.Humanoid = self.Character:WaitForChild("Humanoid")
    self.Path = PathfindingService:CreatePath({
        AgentCanJump = false,
    })

    task.spawn(function()
        while self.Character:IsDescendantOf(game) and self.Character:FindFirstChild("Humanoid") do
            local ClosestPlayer, ClosestPlayerDistance
            for _, Player in pairs(Players:GetPlayers()) do
                local Character = Player.Character
                local RootPart = Character and Character:FindFirstChild("HumanoidRootPart")
                if RootPart then
                    local distance = (RootPart.Position - self.Root.Position).Magnitude
                    if not ClosestPlayerDistance or ClosestPlayerDistance > distance then
                        ClosestPlayer, ClosestPlayerDistance = Player, distance
                    end
                end
            end

            if ClosestPlayer then
                if ClosestPlayerDistance > self.Character:GetAttribute("AgroDistance") then
                    self.Path:ComputeAsync(self.Root.Position, ClosestPlayer.Character.HumanoidRootPart.Position)
                    self.Waypoints = self.Path:GetWaypoints()
                    self.NextTicket = 1
                else
                    self.NextTicket = 1
                    self.Waypoints = nil
                end
            else
                self.NextTicket = 1
                self.Waypoints = nil
            end

            task.wait(0.1)
        end
    end)

    task.spawn(function()
        while self.Character:IsDescendantOf(game) and self.Character:FindFirstChild("Humanoid") do
            if self.Waypoints and #self.Waypoints ~= 0 then
                self.Humanoid:MoveTo(self.Waypoints[self.NextTicket]["Position"])
            end
            task.wait(0.1)
        end
    end)

    self.Humanoid.Died:Connect(function()
        self.Humanoid.Health = 100
        self.Character:Clone().Parent = workspace
        self.Character:Destroy()
    end)
end

function Component:Stop()
    -- Nothing to clean but function is required :)
end

Component:BindToTag("Zombie")

return Component

This kind of stuff really helps pathfinding also …

local H = math.huge
local Agents={WaypointSpacing=(5),
	AgentRadius=(1), AgentHeight=(0),
	AgentCanJump=(false), AgentCanClimb=(false),
	Costs={Metal=H, Basalt=H, LeafyGrass=H, SmoothPlastic=H}
}

I thought it would make sense to send a place file so here you guys go
TestingPlace.rbxl (71.3 KB)

Anybody know whats wrong exactly? I am pretty lost on it