Rayasting Problem

So, I started learning about raycasting a few days ago, and I decided to try to make a tool out of it. But, I ran into a slight error:

17:47:47.895 - FindFirstAncestorWhichIsA is not a valid member of Vector3

How would I be able to fix that?

A little snippet of code:

local FrontRay = Ray.new(script.Parent.Position, Vector3.new(0,0,-1))
local LeftRay = Ray.new(script.Parent.Position, Vector3.new(-2,0,0))
local RightRay = Ray.new(script.Parent.Position, Vector3.new(2,0,0))
local TopRay = Ray.new(script.Parent.Position, Vector3.new(0,1,0))
local objectsTouched = {workspace:FindPartOnRay(FrontRay), workspace:FindPartOnRay(LeftRay), workspace:FindPartOnRay(RightRay), workspace:FindPartOnRay(TopRay)}
for i, object in pairs(objectsTouched) do
	if not object == FrontRay or LeftRay or RightRay or TopRay then
		print(object) -- prints until ray is detected
		local objectParent = object:FindFirstAncestorWhichIsA("Model") -- Error here

I tried many things such as ignoring the ray:

workspace:FindPartOnRay(FrontRay, Ray)

I also tried this:

workspace:FindPartOnRay(FrontRay, FronRay)

But it gave me more errors.

I also searched youtube and even looked at the developer hubs raycasting laser tool, but still, I didn’t fix my problem.

I know you can simply create a part and then fire touched events(as of right now I think that), but that would be too messy and would probably use up more time to register(I don’t want that).

How would I go around this?

All help is greatly appreciated.

1 Like

The only thing I can say here is that the FindPartOnRay method actually returns two values. The first one being the hit position of the ray if I’m correct, which is a Vector3 value. The second one would return the actual object it hits, if any.

So here’s how that’d look:

local hit_position, part = workspace:FindPartOnRay(ray) print(part and part.Name)

Hope this helps. :slight_smile:

3 Likes

The issue is I believe as @C_Sharper said, your object table contains the vector3 values and object values, Tuple, returned by the FindPartOnRay method

As another example, FindPartOnRay() returns a tuple. More specifically, it returns a Part and a Vector3

So what that means as an example this is essentially what the FindPartOnRay is returning:

function FindPartOnRay (Ray) -- obviously  this isn't a real function
  return Part, vector3
end


local Objects =  {FindPartOnRay(Ray)} -- A Table of 2 Values, A part and a vector

for i,v in pairs(Objects) do
 print(v) -- first  index is the part, then the second is the vector3
end

One way to fix this is using TypeOf to check if the value in your array is not a vector3:

for i, object in pairs(objectsTouched) do
   if  TypeOf(object) ~=  "Vector3" then-- Or  typeOf(object) == "Instance"
           -----Do whatever 
    end
end

Another thing i noticied is that here:

if not object == FrontRay or LeftRay or RightRay or TopRay then

The Lua compiler might think that you are saying if the object doesn’t equal the front ray or The LeftRay exists/ is true or The RightRay exist/is true and so on then do whatever. If this is the case then wrapping the or statements in parenthesizes should fix the problem

if not object  ==  (FrontRay or LeftRay or RightRay or TopRay) then
2 Likes

Now that that problem is solved, it’s returning as an enum material, but I don’t want it to be an enum, I want it to be the actual object, how can I fix that?

What is returning that, FindPartOnRay?

It returns as Enum.Material.WhateverMaterialThatIsTouched assuming I used typeof

This is because there are other values returned by FindPartOnRay (which i forgot about), the fourth value, the material of the part that the ray hit is also returned. So instead of using typeOf(object) ~= "Vector3" we can use typeOf(object) == "Instance", this should ensure that we are only using instances (parts)

1 Like