Need help with pathfinding (I'm at my limit)

So I made a script

Basically if you click on a unit you select it and if you click on the ground they move there, but the pathfinding is killing it. I’ve been trying for hours now

Here’s my server script, I think everything that’s wrong is in here

local folderownerevent = game.ReplicatedStorage.FolderOwnerEvent
local moveUnitEvent = game.ReplicatedStorage.MoveUnitEvent
local pathfindingService = game:GetService("PathfindingService")

game.Players.PlayerAdded:Connect(function(player)
	local playerFolder = Instance.new("Folder")
	playerFolder.Name = player.Name
	playerFolder.Parent = game.Workspace.Bots

	print(player.Name .. "'s folder created.")
	
	folderownerevent:FireClient(player, playerFolder)
end)

moveUnitEvent.OnServerEvent:Connect(function(player, unit, targetPosition)
	local playerFolder = game.Workspace.Bots:FindFirstChild(player.Name)
	if playerFolder and unit.Parent == playerFolder then

		unit.Humanoid:MoveTo(unit.HumanoidRootPart.Position)
		unit.Humanoid.MoveToFinished:Wait()

		local path = pathfindingService:CreatePath({
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = false,
			AgentJumpHeight = 10,
			AgentMaxSlope = 45,
		})
		
		path:ComputeAsync(unit.HumanoidRootPart.Position, targetPosition)

		local waypoints = path:GetWaypoints()

		for i, waypoint in pairs(waypoints) do
			unit.Humanoid:MoveTo(waypoint.Position)
			unit.Humanoid.MoveToFinished:Wait()

			-- Debug waypoint visualization
			local debugWaypoint = Instance.new("Part")
			debugWaypoint.Size = Vector3.new(1, 1, 1)
			debugWaypoint.Position = waypoint.Position
			debugWaypoint.Anchored = true
			debugWaypoint.CanCollide = false
			debugWaypoint.BrickColor = BrickColor.new("Institutional white")
			debugWaypoint.Transparency = 0.5
			debugWaypoint.Parent = workspace
			game:GetService("Debris"):AddItem(debugWaypoint, 1) -- Remove after 1 second
		end
	end
end)

PLEASE help me, I’m going to loose it

image

4 Likes

Why dont you try to set all the parts to part:SetNetworkOwner(nil) (server)?

Still same issue my friend, i don’t know if i applied it correctly doe
image

image

the server automatically sets the networkownership every frame so you’ll have to do that every frame aswell

last time i checked there’s not a way to prevent the server from automatically choosing the networkownership for each part

I would check all parts like Union and meshparts.

If this doesnt work you can just check the distance between the npc and next position, and if its close enough, loop.

Instead of

unit.Humanoid.MoveToFinished:Wait()

Do

repeat task.wait() until HummanoidRoot.Position - waypoint.Position <= 5

I would do the second option cause MoveToFinished:Wait() is such a bad function because it moves network ownership around to players and to the server.

1 Like

that didn’t work, now they don’t move at all :frowning:

I forgot to put magnitude in! I’m sorry about that.

repeat task.wait() until (HummanoidRoot.Position - waypoint.Position).Magnitude <= 5
2 Likes

This has nothing to do with network ownership. Your main problem is that every time you click, you’re spawning a new thread with its own path and they’re all running and trying to control the same NPC. Each time the server gets a move event for a specific NPC, it should stop that NPC’s current pathfinding thread or queue up to execute when it’s done, whichever is appropriate for your game. But multiple pathfinding threads should not be running for the same NPC all telling it to go different places.

Yeah, I need to know how to force stop it, and I can’t use break or else players will have to click twice and it also ruins the other ai pathfind aswell

This works a LOT better, thanks :smiley:
Anyways, I’m gonna keep trying to find more solutions. If i couldn’t then I’ll mark this : )

Normally, you’d architect this sort of system a bit differently than you have done. An anonymous function running a for loop isn’t a convenient thing to try to interrupt. There are procedural and object-oriented ways to organize this, but in general, you need the server to be storing the state of each NPC somewhere, either in a Dictionary or in OOP objects representing each NPC. Instead of a for loop, it would be better to generate the path for the NPC and bind a function to the MoveToFinished event, which looks to see if the NPC has a path and if so, what the next waypoint is. Whenever you get a new movement request for a particular unit, you could then stop them in place, flush anything that is left in their current path, and start them on a new one.

IIRC there are a bunch of other events and conditions you have to check for to make pathfinding robust. Paths can fail to generate, NPCs can fail to reach their destinations, etc. You have to explicitly code for all possibilities. Even simple NPC systems generally have a state machine or behavior tree driving them for this reason, to keep things sane and well defined.

1 Like

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