'Raycast error: Unable to cast value to Objects

My goal is just to learn more about raycasting, so I did some tests and at the end they all just said "Unable to cast value to Objects. I tried looking up how to fix this, but most of these errors were with GUI. The method I might be using is probably too old, but it was the newest I could find.

I haven't tried anything else because I don't know how to fix it cause I'm still learning about it.

This is my code:

local target = workspace.Target

local ignore = game.Workspace.IgnorePart

local ray = Ray.new(target.Position, Vector3.new(50,50,50))

while wait(1) do
    local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(target, (ignore))
    if hit then
        print("Found!")
    else
        print("No part was found, didn't hit you idiot")
    end
end

I would show my workspace but I can't put images here yet.. If anyone find anything please tell me! I'd like to know where I went wrong since I'm still learning!



Solution 1:[1]

Looking at the docs for FindPartOnRayWithIgnoreList, it looks like you could have two issues. The first parameter is supposed to be your Ray, and the second parameter is expected to be an array of objects, and you've used parentheses instead of curly brackets.

Try this:

local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, {ignore})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1