Pathfinding problem part 2

The character is moving, but rather slow, like shuffling forward instead of full strides

this is the only real working pathfinder ive been able to make, so im not sure what to change, this is the first time i tried this type of pathfinding.

heres the code:

local Players = game:GetService("Players")
local character = script.Parent  

local humanoid = character.Humanoid
local path = pathfindingService:CreatePath({
	AgentHeight = 5; 
	AgentRadius = 3;
	AgentCanJump = true;
}) 
local runService = game:GetService("RunService") 

local waypoints = nil 
local nextwaypointIndex = nil 
local reachedConnection = nil 
local blockedConnection = nil 

local function findTarget() 
	local max_distance = math.huge 
	local nearestTarget = nil 
	
	for index, player in pairs(Players:GetPlayers())  do
		if player.Character then 
			local target = player.Character 
			local distance = (character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude 
			
			if distance < max_distance  then
				nearestTarget = target 
				max_distance = distance 
			end
			
			if distance < 5  then
				target.Humanoid:TakeDamage(5)
			end
		end
	end
	return nearestTarget
end 

local function followPath(destination)
	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination)
	end)
	
	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints() 
		
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedConnection >= nextwaypointIndex then 
				
				blockedConnection:Disconnect() 
				findTarget(destination)
			end
		end)
		
		if reachedConnection then 
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextwaypointIndex < #waypoints then 
					
				nextwaypointIndex += 1
				humanoid:MoveTo(waypoints[nextwaypointIndex].Position)
				else
				reachedConnection:Disconnect() 
				blockedConnection:Disconnect()
				end
			end)
		end
		
		nextwaypointIndex = 2
		humanoid:MoveTo(waypoints[nextwaypointIndex].Position)	 	
	else 
		warn("no path", errorMessage)
	end
end 

while wait() do 
	local target = findTarget() 
	if target then
		print(target.Name) 
		followPath(target.HumanoidRootPart.Position)
	end
end```

Since no one is talking here and I don’t have a certain answer. I’m guessing that you didn’t add a delay to the loop, you can try adding a 0.1 delay to the wait() loop and if this doesn’t work you can try setting the network ownership to the server so that the server does the physics.

1 Like

nope, same problem im afraid… is there any other possible solution

You want the character to move smooth? your humanoid.MoveToFinished function might be causing the character to shuffle. Maybe deleting it might fix the shuffle problem

same problem… what could possibly be doing this

I tested this script and removed the reachedConnection variable, the humanoid.MoveToFinished Function as well and changed the network ownership to the server and it seem to move smoothly, are you sure there is something in your character that seems to cause a problem?

local Players = game:GetService("Players")
local character = script.Parent  

local humanoid = character.Humanoid
local path = pathfindingService:CreatePath({
	AgentHeight = 5; 
	AgentRadius = 3;
	AgentCanJump = true;
}) 
local runService = game:GetService("RunService") 

local waypoints = nil 
local nextwaypointIndex = nil 
local blockedConnection = nil 
character.PrimaryPart:SetNetworkOwner(nil)
local function findTarget() 
	local max_distance = math.huge 
	local nearestTarget = nil 
	
	for index, player in pairs(Players:GetPlayers())  do
		if player.Character then 
			local target = player.Character 
			local distance = (character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude 
			
			if distance < max_distance  then
				nearestTarget = target 
				max_distance = distance 
			end
			
			if distance < 5  then
				target.Humanoid:TakeDamage(5)
			end
		end
	end
	return nearestTarget
end 

local function followPath(destination)
	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination)
	end)
	
	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints() 
		
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedConnection >= nextwaypointIndex then 
				
				blockedConnection:Disconnect() 
				findTarget(destination)
			end
		end)
		
		nextwaypointIndex = 2
		humanoid:MoveTo(waypoints[nextwaypointIndex].Position)	 	
	else 
		warn("no path", errorMessage)
	end
end 

while wait() do 
	local target = findTarget() 
	if target then
		print(target.Name) 
		followPath(target.HumanoidRootPart.Position)
	end
end

Could you maybe send a video of this “shuffling” problem?

The pathfinding is a bit taxing on the server and can take a while to really process, the delay should be longer especially if it actually finds the path.

Try setting the primary part’s or humanoidrootpart’s network of the NPC to nil? for example:
script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

this will make it not move at all, its has some weird delicate effect where removing anything that would normally not affect the code make the model stop in its tracks.

What about TweenService?

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local character = script.Parent
local humanoid = character.Humanoid
local pathfindingService = game:GetService("PathfindingService")

local movementDuration = 0.5  

local function findTarget()
    
end

local function moveTo(position)
    local targetPosition = Vector3.new(position.X, character.Position.Y, position.Z)
    local distance = (targetPosition - character.Position).Magnitude

    local tweenInfo = TweenInfo.new(distance / humanoid.WalkSpeed, Enum.EasingStyle.Linear)
    local tween = TweenService:Create(character.PrimaryPart, tweenInfo, { Position = targetPosition })
    tween:Play()
    tween.Completed:Wait()
end

while wait() do
    local target = findTarget()
    if target then
        moveTo(target.HumanoidRootPart.Position)
    end
end

Thanks, this removed the buffering, which is the problem I was mainly trying to fix, so we could say thats wrapped up, im sure that other threads can help me with my other minor issues
e.g being able to follow the player endlessly

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