Raycasting Not detecting player in-front

I was doing an npc that detects a player in front and increases their npc speed, but it doesn’t seem to work, are there possible reasons?

local NPCModel = script.parent
local endPosition = script.Parent:WaitForChild("Endpos")


local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {NPCModel, endPosition, script.Parent.Head}
raycastParams.IgnoreWater = true

-- Cast the ray
while wait(0.1) do
for i,v in pairs(game:GetService("Players"):GetPlayers()) do

local raycastResult = workspace:Raycast(script.Parent.Head.Position, endPosition.Position, raycastParams)

-- Interpret the result
if raycastResult then
		print("Got Player")
			
		repeat wait(0.15) NPCModel.Humanoid.WalkSpeed += 1 until NPCModel.Humanoid.WalkSpeed == 125
		print("Hit position:", raycastResult.Position)

    else
	print("Nothing was hit!")
	end
	end
end

What is “Endpos”? workspace:Raycast() recieves an origin and a direction, not an ending position. If I would have to detect something right in front of the character with Raycast I would use HumanoidRootPart and its CFrame.

local Result = workspace:Raycast(HRP.Position, HRP.CFrame.LookVector, Params)


In my model, the EndPost, its the range, something like that

Try using their Unit vector difference to set direction.

local Diff = (HRP.Position-EndPosition)
local Distance = Diff.Magnitude
local Result = workspace:Raycast(HRP.Position, Diff.Unit*Distance, Params)

So, if i have NPC Lookvector calculated, how i would detect the player in front of it?, i mean, how i can set the player character, i noticed that i , v in pairs(game:GetService("Players"):GetPlayers()) doesn’t works

edit : nevermind, i got it work, thanks !!

You should use Unit vector as Sarchyx says, but I never had any luck using a variable for the unit, myself… I had to use a constant: 7. Probably something stupid i was doing…?

And don’t loop though Players. Just cast one ray, an check what it hit…

Here is a model, which pathfinds using rays, made b4 Pathfinding existed…
Canabalize it…

Speed AI by RealAi Inc. - Brains 4 Zombies - Roblox

oh, and always draw the ray too, as a visual aid for yourself…