How to make ray*cast* follow a player?

I do not know what I’m doing wrong but when I’m trying to make a raycast follow a player nothing happens. I’ve lookd up several solutuions however all of them use the deprecated versions.
This is the most crucial part of the code I’m using:

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true

RunService.RenderStepped:Connect(function()
	local NPCHEAD = NPC.Head.Position
	local PlayHead = Player.Head.Position
	local raycastResult = workspace:Raycast(NPCHEAD, PlayHead, raycastParams)
	if raycastResult then
		if raycastResult:IsAncestorOf(Char) then
			game.Workspace.Baseplate.BrickColor = BrickColor.new("Really red")
		else 
			game.Workspace.Baseplate.BrickColor = BrickColor.new("Lime green")
		end
	end
end

I left out some variables to keep the snippet short, all variables are defined and no error code is displayed.

1 Like

Why are you using IsAncestorOf ?

1 Like

The API documentation says that the second parameter is direction, not target.
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
So you need to give it a direction:

local raycastREsult = workspace:Raycast(NPCHEAD, (PlayHead-NPCHead).Unit * 200, raycastParams)

I’m trying to check if there’s a part between the player and origin

As @JarodOfOribter says it requires a direction so simply passing a position won’t work

local raycastREsult = workspace:Raycast(NPCHEAD,(NPCHead-PlayerHead), raycastParams)

However, i do not recommend Jarod use of Multiplying by 200, since the distance between 2 points is not gunatureed to be 200 studs

1 Like