Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can't detect what the part's name is, in the raycast?

Asked by 3 years ago

My Raycast won't detect what the part's Name is ? - If that makes sense.

local basePlate = game.Workspace.basePlate

local rayOrigin = game.Workspace.Part.Position

local rayDirection = Vector3.new(0,30,0)


local raycastParams = RaycastParams.new()

raycastParams.FilterDescendantsInstances = {basePlate}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResults = workspace:Raycast(rayOrigin, rayDirection, raycastParams)



while wait(0.1) do
    local hit = raycastResults.Instance

    print(hit.Name) 

end

The error code that i'm getting : attempt to index nil with 'Instance'

1 answer

Log in to vote
1
Answered by 3 years ago

A ray is a unitray by default meaning it only extends by one stud. You have to set it’s length by multiplying the direction vector by the length of the ray you want. For example do rayDirection * the length you want. Also wrap the print in an if statement so that it does not error out if it returns nil: if raycastResults then

Ad

Answer this question