Need help...pathfinder...NEED HELP

THIS DOESNT WORK at all I really need help Im not sure what to do anymore I also want to change this to follow the player and when the player hides the monster (jhon) stops chasing… Is that so hard??? heres the script

local jhon = script.Parent
local HumanoidRootPart = jhon.Torso



local params = RaycastParams.new()

params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {game.Workspace:WaitForChild("Baseplate")}
while task.wait(0.1) do
local direction = (HumanoidRootPart.Position - jhon.).Unit
local result = workspace:Raycast(jhon.Position,direction * 100, params)

	if result and result.Instance then
	if result.Instance == HumanoidRootPart then
		HumanoidRootPart.color = Color3.fromRGB (255, 0, 4)
	else
		HumanoidRootPart.color = Color3.fromRGB(93,83,142)
	end
else
	HumanoidRootPart.color = Color3.fromRGB( 93,83,142)	
	end
end
2 Likes

Please provide more details to what you want to achieve and what is the issue

hello thanks for reading and responding well I just have no clue why but this script doesnt work at all i was hoping someone else either knew whats the problem or have another system for me to try im trying to make an enemy chase a player but then the player can hide and stop getting chased ive really been struggling with raycast is there another easier solution???

Is there any errors?
I notice in the line local direction = (HumanoidRootPart.Position - jhon.).Unit you have jhon.

I am assuming the line is meant to be local direction = (HumanoidRootPart.Position - jhon.Position).Unit as looking at the code, it will error.

well all there is is "Position is not a valid member of Model “Workspace.jhon” also he doesnt do anything when i join it in studio and test it he just stands there

Right, so this is because he is a model.
You have two options:

  • Find a part (Best bet is a primary part. For example, character models of players have a HumanoidRootPart) and get the position from that.
  • Get the Pivot of the model, and get the position from the CFrame.

I see you have the HumanoidRootPart of jhon, which would be his position. But then you are subtracting something (which I guessed was his position).

If you are wanting it to go after a player I am guessing it should be the closest HumanoidRootPart from a player model - Not jhon.

so i would need to change it to Player humanoidrootpart? then for jhon I would change it to a primary part other than humanoidrootpart?

As I can see by your code, you are making jhon chase himself

1 Like

so i just need to change that? and how??

That line is total mess:
local direction = (HumanoidRootPart.Position - jhon.).Unit
because you are subtracting the jhon which is the model from his position which doesnt make sense at all

if this is a single player game then this should work

local jhon = script.Parent
local HumanoidRootPart = jhon.Torso
local Player = game.Players:FindFirstChildOfClass("Player") or game.Players.PlayerAdded:Wait()
local PlayerRootPart = Player.Character.HumanoidRootPart


local params = RaycastParams.new()

params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {game.Workspace:WaitForChild("Baseplate"), jhon}
while task.wait(0.1) do
local direction = (PlayerRootPart.Position-HumanoidRootPart.Position).Unit
local result = workspace:Raycast(jhon.Position, direction * 100, params)

	if result and result.Instance then
	if result.Instance == PlayerRootPart then
		HumanoidRootPart.color = Color3.fromRGB (255, 0, 4)
	else
		HumanoidRootPart.color = Color3.fromRGB(93,83,142)
	end
else
	HumanoidRootPart.color = Color3.fromRGB( 93,83,142)	
	end
end

well sadly it isnt but could this be changed for a multiplayer game?

local jhon = script.Parent
local HumanoidRootPart: Part = jhon.Torso


local params = RaycastParams.new()

params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = {game.Workspace:WaitForChild("Baseplate"), jhon}
while task.wait(0.1) do
	local foundPlayers = {}
	for _, i in game.Players:GetPlayers() do
		if i.Character then
			local PlayerRootPart = i.Character.HumanoidRootPart
			local direction = (PlayerRootPart.Position-HumanoidRootPart.Position).Unit
			local result = workspace:Raycast(jhon.Position, direction * 100, params)
			if result and result.Instance then
				if result.Instance:IsDescendantOf(i.Character) then
					table.insert(foundPlayers, i.Character)
				end
			end
		end
	end
	
	local closest = nil
	local closestDistance = 200
	for _, i in foundPlayers do
		local PlayerRootPart: Part = i.HumanoidRootPart
		if (PlayerRootPart.Position-HumanoidRootPart.Position).Magnitude < closestDistance then
			closest = i
			closestDistance = (PlayerRootPart.Position-HumanoidRootPart.Position).Magnitude
		end
	end
	
	if closest then
		HumanoidRootPart.Color = Color3.fromRGB (255, 0, 4)
	else
		HumanoidRootPart.Color = Color3.fromRGB(93,83,142)
	end
end
1 Like

i really cant thank you enough man this actually really helped!! and thanks to everyone in this forum!!!
like i really hope your days are amazing!

2 Likes