Simple Path Enemy

Hello, this script isn’t working and the only error I get is

ServerStorage.SimplePath:25: SimplePath Error: Pathfinding target must be a valid Vector3 or BasePart


	--Import the module so you can start using it
	local ServerStorage = game:GetService("ServerStorage")
	local SimplePath = require(ServerStorage.SimplePath)

	--Define npc
	local Dummy = script.Parent

	-- Define a part called "Goal"
	local Goal = workspace.Part

	--Create a new Path using the Dummy
	local Path = SimplePath.new(Dummy)

	--Helps to visualize the path
	Path.Visualize = true



local npc = script.Parent
closestPlayer = nil
closestDistance = 200
function getClosestPlayer()
	
	for i, player in pairs(workspace:GetChildren()) do
		if player.Humanoid.Health ~= 0 and player.Name ~= Dummy then
			local distance = (npc.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
			if distance < closestDistance then
				closestPlayer = player
				closestDistance = distance
			end
		end
	end
end	

while true do

		Path:Run(closestPlayer)
	end

Doing closestPlayer.HumanoidRootPart.Position or anything like that doesn’t work as well please help thx

Consider verifying whether or not the player is nil.

Additionally, there is already a built-in function in simplepath that finds the player who is closest to the NPC. Try using it

In this code below:

while true do
	Path:Run(closestPlayer)
end

It’s possible that closestPlayer is equal to nil, since it starts as nil. Instead of simply running it, add a condition.
Also, unless Path:Run() has a yield inside it, this will be an infinite unyielding loop, which will simply crash any server that it runs in. Consider adding a delay too:

while true do
	if closestPlayer then
		Path:Run(closestPlayer)
	end
	task.wait(0.75)
end
1 Like

I no longer gives an error but the dummy doesn’t move?

You probably need to give the Path:Run() function an actual part, since right now I think it’s passing the character model. Also, instead of iterating over all of the workspace, try iterating over just players.

function getClosestPlayer()
	
	for i, player in pairs(game:GetService("Players"):GetPlayers()) do
		local Character = player.Character
		local PrimaryPart = Character and Character.PrimaryPart
		local hum = Character and Character:FindFirstChildOfClass("Humanoid")

		if PrimaryPart and hum and (hum.Health > 0) then
			local distance = (npc.PrimaryPart.Position - PrimaryPart.Position).Magnitude
			if distance < closestDistance then
				closestPlayer = Character
				closestDistance = distance
			end
		end
	end
end	

while true do
	if closestPlayer and closestPlayer.PrimaryPart then
		Path:Run(closestPlayer.PrimaryPart)
	end
	task.wait(0.75)
end
1 Like

I was still having problems when I put in the code but turns out I just had a dumb mistake in the code, I forgot to actually call the function in the while loop but your code was correct anyways thx so much

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