I have a script that checks whether the player is in vision cone and I am aiming to check whether the player is blocked by an object like a Part (sending a ray from npc head to chr head and check if something is blocking it).

Thanks in advance!

local rs = game:GetService("RunService")

local npc = workspace.NPC
local chr = game.Players.LocalPlayer.Character

rs.RenderStepped:Connect(function()
	local npcToCharacter = (chr.Head.Position - npc.Head.Position).Unit
	local npcLook = npc.Head.CFrame.LookVector
	
	local dotProduct = npcToCharacter:Dot(npcLook)

	if dotProduct > .7 then
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Whitelist
		rayParams.FilterDescendantsInstances = {chr}
		
		local ray = workspace:Raycast(npc.Head.Position, chr.Head.CFrame.LookVector * 100, rayParams)
		
		if ray then
			print(ray.Instance)
			workspace.Baseplate.BrickColor = BrickColor.new("Bright red")
		else
			workspace.Baseplate.BrickColor = BrickColor.new("Bright green")
		end
	else
		workspace.Baseplate.BrickColor = BrickColor.new("Bright green")
	end
end)

1 comment

0

What is wrong with your script? Are you getting an error? Is it just not working?