Issue with Using a for loop for Pathfinding

Hello,

So I have been trying to use a for loop for pathfinding on a bunch of NPC’s using CollectionService, I have been using This Documentation and This Video to test, but it doesn’t work when I do so.

I have made sure the NPC could Move, and Applied the Tag, Nothing Happens, I have been using RunService.Stepped for the Pathfinding Part.
Current Code:

Server.PrePhysics:Connect(function()
	for _,mob in pairs(MobH.TaggingService:GetTagged("mob")) do
		task.spawn(function()
			
			local waypoints
			local Next
			local ReachedConnection
			local BlockedConnection
			
			local function GetTarget()
				local MaxDistance = MobH.MaxDistance
				local Nearest
				
				for _,Player in game.Players:GetPlayers() do
					if Player.Character then -- Gets Player
						local target = Player.Character
						local distance = (mob.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
						if distance < MobH.MaxDistance then
							Nearest = target
							MaxDistance = distance
						end
						
						if distance < mob:GetAttribute("Range") then -- Damage
							if not mob:GetAttribute("CoolingDown") then
								Nearest.Humanoid:TakeDamage(mob:GetAttribute("Damage"))
								mob:SetAttribute("CoolingDown", true)
								task.wait(mob:GetAttribute("Cooldown"))
								mob:SetAttribute("CoolingDown", false)
							end
							
						end
					end
				end
				
				return Nearest
			end
			
			local function Follow(Location)
				local Success, Err = pcall(function()
					MobH.Path:ComputeAsync(mob.HumanoidRootPart.Position, Location)
				end)
				
				if Success and MobH.Path.Status == Enum.PathStatus.Success then -- Successful Path
					waypoints = MobH.Path:GetWaypoints() -- Waypoints
					BlockedConnection = MobH.Path.Blocked:Connect(function(Blocked) -- Blocked Connection
						if Blocked >= Next then
							BlockedConnection:Disconnect()
							Follow(Location)
						end
					end)
					
					if not ReachedConnection then
						ReachedConnection = mob.Humanoid.MoveToFinsished:Connect(function(Reached) -- Connection
							if Reached and Next < #waypoints then
								Next += 1
								mob.Humanoid:MoveTo(waypoints[Next].Position)
							else
								ReachedConnection:Disconnect()
								BlockedConnection:Disconnect()
							end
						end)
					end
					
					Next = 2
					mob.Humanoid:MoveTo(waypoints[Next].Position)
				end
			end
			
			if mob then
				
				local target = GetTarget() -- Gets Target
				
				if target then
					Follow(target.HumanoidRootPart.Position) -- Follows Player
				end
				
				
				mob.HumanoidRootPart:SetNetworkOwner(nil) -- Network Ownership
			end
			
			
			
			
		end)
	end
end)
end)

Sorry for little info
Not sure what I’m doing wrong, or what part

1 Like

I fixed the Issue, i made a bunch of Spelling mistakes and i had renamed the Humanoid

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.