Possible to add path finding to this following these rules?

(I’m pretty sure this is the right category to put this topic, if not please correct me so I can change)

Hi everyone!
My little brother gave me the challenge of writing a script, that makes something chase after you, but:

  • I can’t use pathfinding
  • I can’t use magnitude/distance
  • And I can’t change the position/CFrame directly using my code

I’ve tried searching it up but too no luck.

Here’s my script (ignore raycasting for now):

local part = script.Parent
local fireRate = 0.1 -- seconds
while wait(fireRate) do
	for i, v in pairs(game.Workspace:GetChildren()) do -- loops through workspace children
		local human = v:FindFirstChild("Humanoid")
		local hRoot = v:FindFirstChild("HumanoidRootPart")
		local head = v:FindFirstChild("Head")
		if human and hRoot and head then -- if a game child has a: humanoid, humanoidrootpart, and head then continue
			local Raycast1 =  Ray.new(part.Position, part.CFrame.LookVector * 10)
			local hit = game.Workspace:FindPartOnRayWithIgnoreList(Raycast1, {part})
			part.CFrame = CFrame.new(part.Position, hRoot.Position + Vector3.new(0, 15, 0))
			if hit then
				print("ray found "..hit.Name)
			else
				print("ray found nothing")
			end
		end
	end
end

How it works is, the part looks above the player, it falls slightly, then it comes back up.
Kind of like a snake but not really lol


He said that it doesn’t go around walls…
DUDE I-

is it even possible to implement pathfinding to this? (following those rules)
If so that might be fun to do as long as it’s not SUPER complicated


why I added raycasting

I thought that maybe if it hit a wall and that the wall’s name isn’t equal to a name of a player
it could somehow go around it?
(I added raycasting and not just a touch event bc I don’t want this to push around unanchored parts by accident)


my question is:
is this possible?
I NEVER turn down a challenge (that’s possible)

(for those who are confused: it’s just a block with a face at the front lol)

You could just add a part with a body velocity and an align orientation inside it so it turns when the target turns, simple.

but wouldn’t that mean that if the block was a ways behind me,
then I went out around a wall, but then went back in, wouldn’t he still get stuck on the wall i just passed?

Technically anything could get stuck if you try hard enough however if you use a ball as the part it’ll be very unlikely to get stuck in the first place.
i wouldn’t worry about it getting stuck unless you use a metal cube.

what I’m saying is that i don’t want the ball to collide with the obstacle at hand.
If there is a wall, then i want it to go around it, without colliding, because if it hits an unanchored obstacle, then it will push it, and a ball wouldn’t work with my script, it has to be literally anything but a sphere.

update:
after a little break i edited my script to detect when there is an obstacle in front of it.

local part = script.Parent
local fireRate = 0.1 -- seconds
while wait(fireRate) do
	for i, v in pairs(game.Workspace:GetChildren()) do -- loops through workspace children
		local human = v:FindFirstChild("Humanoid")
		local hRoot = v:FindFirstChild("HumanoidRootPart")
		local head = v:FindFirstChild("Head")
		if human and hRoot and head then -- if a game child has a: humanoid, humanoidrootpart, and head then continue
			local Raycast1 =  Ray.new(part.Position, part.CFrame.LookVector * 10)
			local hit = game.Workspace:FindPartOnRayWithIgnoreList(Raycast1, {part})
			part.CFrame = CFrame.new(part.Position, hRoot.Position + Vector3.new(0, 15, 0))
			if hit then
				print("ray found "..hit.Parent.Name)
				if hit.Parent:FindFirstChild("Humanoid") then
					print("looking at Humanoid")
				else
					print("looking at obstacle")
				end
			else
				print("ray found nothing")
			end
			
			
			
			
			
			
		end
	end
end

I wrote a script to do exactly this for another forum member It clones an NPC from ServerStorage every 5 seconds and controls them all using CollectionService.

OP wants to not use built in pathfinding, I assume this is a coding challenge to see if it can be done, not asking how to do it in the easiest possible way (which would be using the built in stuff)…

If you need to be able to chase someone, going round walls, but without using the built in pathfinding you could look at implementing A*, a custom pathfinding algorithm. It’s not SUPER complicated, but is a bit of a challenge.

You’d probably need to convert the map into a grid and can then use that for the pathfinding

1 Like

thanks! I’ll try this.
And yeah this is what I meant!

1 Like