I'm new to rays and I'm currently trying to figure it right now, but I got stuck on this problem:

local UserInputService = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
local viewportPoint = camera.ViewportSize / 2
local unitRay = camera:ViewportPointToRay(viewportPoint.X, viewportPoint.Y, 0)
local length = 500

local function getIntersection(part, ray)
	local whiteList = {part}
	local _, position, normal = workspace:FindPartOnRayWithWhitelist(ray, whiteList)
	return position, normal
end

local function inputBegan (input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
		print(getIntersection(workspace.Parts:GetChildren()))
	end
end

UserInputService.InputBegan:Connect(inputBegan)

On line 10, I'm getting the error "Unable to cast value to object", It's probably how I'm assigning the variable or if there's something wrong with how I wrote the code itself.

My goal is to get the part in the center of the screen and that part should be a child of the model "parts", all child in the model is named the same.

2 comments

1

The error is because on line 17 you are passing table and on line 9 you are putting that table inside of another table, it creates nested table which i think are not allowed for that. Also use workspace:Raycast() instead, this function has been deprectaed.

0

I see thanks!