How To Make A Humanoid Chase The Closest Player?

messed around with some stuff and this seems to work

local Pathfind = game.PathfindingService

while wait(0.1) do
	local human = script.Parent.Humanoid
	local pos = human.RootPart.Position
	local target = nil
	local targetDistance = math.huge
	local path = Pathfind:CreatePath({["WaypointSpacing"] = 32})
	local op = OverlapParams.new()
	op.FilterType = Enum.RaycastFilterType.Exclude
	op.FilterDescendantsInstances = {script.Parent}
	
	for _, i in workspace:GetPartBoundsInRadius(pos, 1000, op) do
		if i.Name == "HumanoidRootPart" then
			if (i.Position-pos).Magnitude < targetDistance then
				path:ComputeAsync(pos, i.Position)
				if path.Status == Enum.PathStatus.Success then
					target = i.Position
					targetDistance = (i.Position-pos).Magnitude
				end
			end
		end
	end
	
	if target then
		path:ComputeAsync(pos, target)
		local waypoint = path:GetWaypoints()[2]
		if waypoint.Action == 1 then
			human.Jump = true
		end
		human:MoveTo(waypoint.Position)
	end
end
2 Likes

Thank you! It Works! Now I Can Work On The Game More

1 Like

this seems to make climbing work

local Pathfind = game.PathfindingService

local prevWaypoint = Vector3.zero

while wait(0.1) do
	local human = script.Parent.Humanoid
	local pos = human.RootPart.Position
	local target = nil
	local targetDistance = math.huge
	local path = Pathfind:CreatePath({["WaypointSpacing"] = 32, ["AgentCanClimb"] = true})
	local op = OverlapParams.new()
	op.FilterType = Enum.RaycastFilterType.Exclude
	op.FilterDescendantsInstances = {script.Parent}
	
	for _, i in workspace:GetPartBoundsInRadius(pos, 1000, op) do
		if i.Name == "HumanoidRootPart" and game.Players:GetPlayerFromCharacter(i.Parent) then
			if (i.Position-pos).Magnitude < targetDistance then
				path:ComputeAsync(pos, i.Position)
				if path.Status == Enum.PathStatus.Success then
					target = i.Position
					targetDistance = (i.Position-pos).Magnitude
				end
			end
		end
	end
	
	if target then
		pos = human.RootPart.Position
		path:ComputeAsync(pos, target)
		if path.Status == Enum.PathStatus.Success then
			local waypoint = path:GetWaypoints()[2]
			if waypoint then
				if prevWaypoint ~= waypoint.Position then
					prevWaypoint = waypoint.Position
					if waypoint.Action == 1 then
						human.Jump = true
					end
					local move = Vector3.new((waypoint.Position-pos).X, 0, (waypoint.Position-pos).Z)
					if move ~= Vector3.zero then
						human:Move(move.Unit)
					end
				end
			end
		end
	end
end
3 Likes

Huh, I Have A Question, Why does it break when it goes in small areas?

AI:31: attempt to index nil with 'Position'

its might be that their just wasn’t a 2nd waypoint for it to travel to

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