Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I have an error with raycasting it says unable to cast dictionary to ray?

I have a problem with raycasting it says: Unable to cast dictionary to ray. heres the script:

local part = script.Parent

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

while wait(1) do
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(Ray,{part})
	if hit then
		print("Raycast")
	else
		print("No part found!")
	end
end

1 answer

That usually means something is unsupported in the table you gave it or it might require an array but I don't see any of those problems in your code.

You can instead try the new raycasting API since you'd probably want to utilize newer and neater API here.

Here's what I recreated of your script with new API:

local part = script.Parent

local origin, direction = part.Position, Vector3.new(0,0,50)
local rayParams = RayParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {part}

while true do
    local result = workspace:Raycast(origin, direction, rayParams)
    if result then
        print("Raycast")
    else
        print("No part found!")
    end
    wait(1)
end

Note that there is a difference in using this API and that is, the result will be nil if there is no instance found whereas the Ray.new() raycasting would return a position always even if no object was found. You also have to index the result like a dictionary in order to see what object was found, the position, normal, etc. (result.Instance, result.Normal, result.Position)

This discussion is preserved as it was. New answers, comments, and votes are closed.