CollectionService not working for NPC pathfinding

Hi guys im new to CollectionService and im having trouble with pathfinding AIs. The NPCs don’t move at all when they get added

local CollectionService = game:GetService("CollectionService")
local PathfindingService = game:GetService("PathfindingService")
local Debris = game:GetService("Debris")

local gizmo = require(game.ReplicatedStorage.gizmo)

local waypointFolder = game.Workspace.Waypoints.Ped_Waypoints:GetChildren()

local agentParameters = {
	Costs = {
		Sidewalk = 0.01
	}
}

function debugPath(waypoints)
	for i, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.CanCollide = false
		part.CanTouch = false
		part.CanQuery = false
		part.Anchored = true
		part.Position = waypoint.Position 
		part.Parent = game.Workspace
		Debris:AddItem(part, 3)
	end
end

local function runTo(endingPart, npc)
	local human = npc.Humanoid
	local torso = npc.Torso
	local path = PathfindingService:CreatePath(agentParameters)
	
	local endPos = Vector3.new(endingPart.Position.X + math.random(-5, 5), endingPart.Position.Y + math.random(-5, 5), endingPart.Position.Z + math.random(-5, 5))
	
	path:ComputeAsync(torso.Position, endPos)
	local waypoints = path:GetWaypoints()
	
	--debugPath(waypoints)
	
	for i, waypoint in pairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			human:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		local rayOrigin = torso.Position
		local rayDirection = torso.CFrame.LookVector * 10
		
		--gizmo.drawRay(rayOrigin, rayDirection)
		
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
		
		local pathChanged = false
		if (raycastResult and raycastResult.Instance.Parent:FindFirstChild("Humanoid")) then
			--print(raycastResult.Instance.Parent)
			local part = Instance.new("Part")
			part.CanCollide = false
			part.CanTouch = false
			part.CanQuery = false
			part.Anchored = true
			part.Position = waypoint.Position
			part.CFrame = torso.CFrame * CFrame.new(-7,0,-10)
			part.Parent = game.Workspace
			part.Transparency = 1
			Debris:AddItem(part, 3)
			waypoint = PathWaypoint.new(part.Position, Enum.PathWaypointAction.Walk)
			pathChanged = true
		end
		
		human:MoveTo(waypoint.Position)
		human.MoveToFinished:Wait(0.01)
		if pathChanged then
			runTo(endingPart)
			break
		end
	end
end

for i, v in pairs(CollectionService:GetTagged("Pedestrian")) do
	while true do
		runTo(waypointFolder[math.random(1, #waypointFolder)], v)
	end
end
local CollectionService = game:GetService("CollectionService")

local tag = "YourTag" -- Replace with your desired tag

local function onTagAdded(instance)
    -- Your code to handle when an instance with the specified tag is added
    print(instance.Name .. " with tag " .. tag .. " added!")
end

local function onTagRemoved(instance)
    -- Your code to handle when an instance with the specified tag is removed
    print(instance.Name .. " with tag " .. tag .. " removed!")
end

-- Connect the functions to the tag events
CollectionService:GetInstanceAddedSignal(tag):Connect(onTagAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onTagRemoved)

-- Example: Manually add a tag to a part
local part = workspace.Part -- Replace with your desired part
CollectionService:AddTag(part, tag)

1 Like

thanks a lot!!!

1 Like

Welcome you no problem enjoying

Did you working on run about good

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