Raycasting Is Not Working For Me

The Code:
local ignore = {}
for i,v in pairs(game.Players.LocalPlayer.Character:GetChildren()) do
if v:IsA(“BasePart”) then
table.insert(ignore,v)
end
end

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {ignore}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true


local rayOrigin = game.Workspace.CurrentCamera.CFrame.Position
local rayDirection = Vector3.new((mouse.Hit.X-game.Workspace.CurrentCamera.CFrame.Position.X)*3+mouse.Hit.X
	, (mouse.Hit.Y-game.Workspace.CurrentCamera.CFrame.Position.Y)*3+mouse.Hit.Y
	, (mouse.Hit.Z-game.Workspace.CurrentCamera.CFrame.Position.Z)*3+mouse.Hit.Z
)

local raycastResult = nil
for i = 1,100 do
	wait()
	if raycastResult == nil then
		raycastResult = workspace:Raycast(rayOrigin, rayDirection,raycastParams)
	else
		break
	end
end

Screenshot 2024-01-14 163812
This is the raycast

cant you set raycastParams.FilterDescendantsInstances = {ignore} into raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}

also your breaking the loop while the raycastResult is nil

1 Like

Where are you trying to raycast to? If you want to raycast to the mouse position, use the Unit ray.

Code:

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true

local rayOrigin = workspace.CurrentCamera.CFrame.Position
local rayDirection = (rayOrigin - mouse.Hit.Position).Unit

local raycastResult = nil
for i = 1,100 do
	task.wait()
	if raycastResult == nil then
		raycastResult = workspace:Raycast(rayOrigin, rayDirection * 100, raycastParams)
	else
		break
	end
end