Raycasting is not working

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {Eye1, Eye2, Torso, HRP}

local rayDirection = Origin.CFrame * CFrame.Angles(0, 0, math.rad(Angle.Value))
local ray = workspace:Raycast(Origin.Position, rayDirection.LookVector * Distance.Value, raycastParams)

while true do
	task.wait(0.1)
	if ray then 
		if Players:GetPlayerFromCharacter(ray.Instance.Parent) then
			local player = Players:GetPlayerFromCharacter(ray.Instance.Parent)
			if player then
				if player.Character.Humanoid.Health > 0 then
					CanSee.Value = true
					warn("I see " .. player.Name)
					break
				else
					CanSee.Value = false
				end
			end
		else
			warn("Not Player")
		end
	end
end

I tried looking for a solution on the dev forum for a while now, but I can’t seem to find one.
This script is for an NPC that will detect a player if in the raycast, but all it says in Output is "Not Player" There is are no errors even though it doesn’t work.

The local rayDirection = Origin.CFrame * CFrame.Angles(0, 0, math.rad(Angle.Value)) line is probably the problem, because I just found this on the forum to calculate an angle, even though I don’t really understand it. (There’s no error so I don’t know…)

Any help is appreciated!

With your current script, a ray is only computed once as the script executes. To fix this issue, move lines 5-6 inside the loop immediately after task.wait(0.1).

task.wait(0.1)

local rayDirection = Origin.CFrame * CFrame.Angles(0, 0, math.rad(Angle.Value))
local ray = workspace:Raycast(Origin.Position, rayDirection.LookVector * Distance.Value, raycastParams)

if ray then

Can you also include the section of your script where the variables Origin and Angle are defined; this could help me determine the necessity of moving line 5 inside the loop or any other issues.

1 Like

Okay I added those lines inside the loop but nothing changed.

Heres’ the full script (with those lines in the loop):

local Players = game:GetService("Players")

local NPC = script.Parent
local Origin = NPC.Head	

local Distance = NPC.SpotDistance
local Angle = NPC.RayAngle
local CanSee = NPC.CanSee

local Eye1 = NPC.Head.Eye1
local Eye2 = NPC.Head.Eye2

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {Eye1, Eye2}

while true do
	task.wait(0.1)
	local rayDirection = Origin.CFrame * CFrame.Angles(0, 0, math.rad(Angle.Value))
	local ray = workspace:Raycast(Origin.Position, rayDirection.LookVector * Distance.Value, raycastParams)
	
	if ray then 
		if Players:GetPlayerFromCharacter(ray.Instance.Parent) then
			local player = Players:GetPlayerFromCharacter(ray.Instance.Parent)
			if player then
				if player.Character.Humanoid.Health > 0 then
					CanSee.Value = true
					warn("I see " .. player.Name)
					break
				else
					CanSee.Value = false
				end
			end
		else
			warn("Not Player")
		end
	end
end

SpotDistance is a number value set to 60, and RayAngle is a number value set to 90.
(Eye1 and Eye2 are accessories attached to the head, so I thought if I filter them out they wouldn’t get in the way, but that wasn’t the problem…)

Try changing line 10 to raycastParams.FilterDescendantsInstances = {NPC}, the ray might be colliding with the NPC’s parts.

1 Like

That didn’t change anything either… I’m not sure why there are no errors, but at the same time nothing is being detected.

Also, do you know how create angles using raycast? I’m trying to make it so that the NPC can see 45 degrees left and right, instead of just a straight line.
The line: local rayDirection = Origin.CFrame * CFrame.Angles(0, 0, math.rad(Angle.Value)) is supposed to make that, but I’m not sure if this is how you do it. I also think this may be the problem, because I copy and pasted this from another post hoping it would help. (But since there is no error I don’t know what to do…)

Try adding print(ray) before if ray then and give me the outputs.

Here’s a screen shot of the output
Screenshot 2024-11-18 181353

Let me know if this script works!

local Players = game:GetService("Players")

local NPC = script.Parent
local Origin = NPC.Head	

local Distance = NPC.SpotDistance
local Angle = NPC.RayAngle
local CanSee = NPC.CanSee

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {NPC}

local function checkIfCharacterIsInFOV(character: Model)
	local playerHead = character:FindFirstChild("Head")
	local playerHumanoid = character:FindFirstChildOfClass("Humanoid")
	
	if
		not playerHead or
		not playerHumanoid or
		not playerHumanoid.RootPart or
		playerHumanoid.Health == 0
	then
		return false
	end
	
	local playerToNPCVector = (playerHead.Position - Origin.Position)
	
	if playerToNPCVector.Magnitude >= Distance.Value then
		return false
	end
	


	return Origin.CFrame.LookVector:Dot(playerToNPCVector.Unit) >= math.cos(math.rad(Angle.Value / 2))
end

while true do
	task.wait(0.1)
	
	for _, player in Players:GetPlayers() do
		if player.Character and checkIfCharacterIsInFOV(player.Character) then
			CanSee.Value = true
			warn("I see " .. player.Name)
		else
			CanSee.Value = false
		end
	end
end
1 Like

Wait that works perfectly now. Thank you!!