Raycast Instance Y Position

Hello!

I was just wondering how to get the raycast instance’s Y position.

This is the raycast code:


	if Raycast and Raycast.Instance then
		print(Raycast.Material or "Couldn't find material.") 
		print(Raycast.Instance)
		rock.Material = Raycast.Material

		local PartColor = Raycast.Instance
		rock.Color = PartColor.Color
		
		if rock then
			rock.Position.Y = Raycast.Instance.Position.Y
		end
	else
		rock.Material = Enum.Material.Slate
		rock.Color = Color3.fromRGB(137, 56, 9)
		print("Couldn't find material.")
	end		

Full error message:

image

It seems like I’m able to raycast just the position, but it bugs out when I try to get the Y pos.

The rock part is supposed to be positioned on the same y axis as the floor.

Any help would be appreciated, thanks.

1 Like

Try this:

rock.Position = Vector3.new(rock.Position.X, Raycast.Instance.Position.Y, rock.Position.Z)
1 Like

To assign a new Position to an object (In this case a BasePart) you must assign a Vector3 object to the Position attribute rather than directly assigning to the position values of the position Vector3. The individual X, Y, Z positions of a Vector3 are read only.

So, to do this:

rock.Position = Vector3.new(rock.Position.X, Raycast.Instance.Position.Y, rock.Position.Z)
1 Like