I am trying to make a slime pathfind the player

Please ask any questions if you need more explaination!

Code:

local rootPart = script.Parent.HumanoidRootPart
local human = script.Parent.Humanoid

function findTarget()
local dist = 200
local target = nil
for i, v in pairs(workspace:GetChildren()) do
	local humanoid = v:FindFirstChild("Humanoid")
	local head = v:FindFirstChild("Head")
	if humanoid and head and v.Name ~= "slime" then
		if (head.Position - rootPart.Position).magnitude < dist and humanoid.Health > 0 then
			dist = (head.Position - rootPart.Position).magnitude
			target = head
		end
	end
end
return target
end
while wait(1) do
local head = findTarget()

if head then
	local ray = Ray.new(rootPart.Position, (head.Position - rootPart.Position).Unit * 200)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, (script.Parent))
	if hit then
		if hit:IsDecendantOf(head.Parent) then
			human:MoveTo(head.Position)
		else
			human:MoveTo(head.Position)
			wait(0.4)
			path = game:GetService("PathfindingService"):FindPathAsync(rootPart.Position,head.Position)
			points = path:GetWaypoints()
			if path.Status == Enum.PathStatus.Success then
				for i,v in pairs(points) do
					human:MoveTo(v:Position)
					human.MoveToFinished:wait(2)
					if v.Action == Enum.PathWaypointAction.Jump then 
						human.Jump = true
					end
					if (points[#points].Position - head.Position).magnitude > 15 then
						break
					end
				end
			else
				human:MoveTo(head.Position)
			end
		end
	end
end
end

Please correct any issues by copying the code and pasting the code corrected, I can’t understand otherwise too well

1 Like

We need to know what doesn’t work about it to hone in on where errors might be.

Here is the error image of the code.
image

It should be v.Position, not v:Position().

1 Like

It shouldn’t be

v:Position()

Must be

v.Position

edit: sorry i didn’t see @CheekySquid’s message.

2 Likes

so human:MoveTo:v.Position ?

human:MoveTo(v.Position)

1 Like

No, what @CheekySquid said above.

1 Like

Thanks but uh now its
image

Test the game now and see what errors you get.

edit: do what @CheekySquid said below then test the game.

1 Like

Path must be intialized. Make it local path = game:GetService("PathfindingService"):FindPathAsync(rootPart.Position,head.Position)

By the way, this is not raytracing. It’s pathfinding.

1 Like

Ok I will change it, Thank you

Sounds good! If it works, be sure to mark the solution so reader’s know it’s been solved.

Its really late right now, I will fix it tommorrow.

1 Like