Raycast for monster is not returning anything when it hits the player

I’m trying to detect if a monster can see a player by casting a ray at the player’s humanoid root part.

The raycast result is empty when the ray hits the player but it does have a result when hitting any other part.

I’ve tried a ton of solutions but there is no error so it’s difficult to find the issue, especially cause I’m new to raycasting.

the stuff printing in output is the part that the ray hits

local spotted = false

while true do


	if script.Parent.RayCastActive.Value == true then
		print("Raycast")
		local raycastresult = workspace:Raycast(script.Parent.Parent.Head.Position, game.ServerStorage.player.Value.Character.HumanoidRootPart.Position - script.Parent.Parent.HumanoidRootPart.Position)
		if raycastresult then
			print(raycastresult.Instance.Name)
			local targetplayer = game.Players:GetPlayerFromCharacter(raycastresult.Instance.Parent)
			if targetplayer then
				if spotted == false then
					spotted = true
					print(targetplayer.Name)
					game.ServerStorage.ServerComunications.HallLurkerJumpScare:Fire("Listen To the audio to make sure the HallLurker is gone")
				end
			end

		end
	end
	wait(0.01)

end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You could try using RaycastParams and changing a few things, e.g

local player = game.ServerStorage.player.Value -- get the player
local monster = script.Parent.Parent -- get the monster
local originP, targetP = monster.Head.Position, player.Character.HumanoidRootPart.Position -- get positions
local dist = (originP - targetP).Magnitude -- distance between monster and player
local direction = (targetP - originP).Unit * dist -- the direction of the ray

local rParams = RaycastParams.new()
rParams.FilterType = Enum.RaycastFilterType.Whitelist -- set filter to whitelist
rParams.FilterDescendantsInstances = {player.Character} -- put player in whitelist
rParams.IgnoreWater = true -- just in case

local raycastresult = workspace:Raycast(originP, direction ,rParams) -- should return result if the monster can "see" the player

(I thought it would be a bit hard to explain so I just wrote this example instead)

Bro, thanks so much!
I think i was messing up the directionpart, Thanks so much. I was legit stuck and this really helped :smiley: