Raycast attempt to call a number value

Hello developers! So I was doing some testing in studio and tried to make a raycasting part
but unfortunately it keeps saying this in the output
image
Here is my code

local part = script.Parent

function createRay()
	local org = part.Position
	local dir = Vector3.new(0, -90, 0)
	local rayParms = RaycastParams.new()
	local rayRes = workspace:Raycast(org, dir, rayParms)
	
	if rayRes then
		local dist = rayRes:Distance(org)
		print(dist)
	end
end

while wait() do
	createRay()
end

local org = part.Position
local dir = Vector3.new(0, -90, 0)
local rayParms = RaycastParams.new()
local rayRes = workspace:Raycast(org, dir, rayParms)

if rayRes then
	local dist = rayRes:Distance(org)
	print(dist)
end

Any help is appreciated!

“:Distance()” is not a method of the RaycastResult returned by Workspace:Raycast(). You get an array containing the Instance, Position, Surface Normal, and the Material you hit.

You could try something along the lines of this:

local part = script.Parent

local function createRay(distance: number)
    local origin = part.Position
    local dir =  Vector3.new(0, -1, 0)
    local rayParams = RaycastParams.new()
    local rayResult = workspace:Raycast(origin, dir * distance, rayParams)
    
    if rayResult and rayResult.Instance:IsA("BasePart") then
        local dist = (rayResult.Instance.Position - origin).Magnitude
        print(dist) -- how far it should've went to hit that BasePart
    end
end

while task.wait() do
    createRay(90) -- max distance of 90
end

Untested code

1 Like

It works! There was just an error because you put origin.Position but I fixed it
Thanks!

Ah shoot, major oversight lol. No problem glad I could help. Gotta edit that in-case anyone else has the same problem :sweat_smile:

1 Like