Problems with raycasting - "Intersect is not a valid member of PhysicsService "PhysicsService""

Hi! I’ve been trying to implement a raycast mechanic to my game so I entered to the forum to look for some help. I copied a script that supposedly worked (I saw the answers of the post) but when i try to implement it to my game I keep getting the same error over and over again: “Intersect is not a valid member of PhysicsService “PhysicsService””.

imagen_2024-02-01_135230876
imagen_2024-02-01_135253660

I am not too good at scripting and I’ve looked everywhere to see if someone else had the same problem but I couldn’t find anything.

This is my script, I wantet to detect if the raycast collided with an object:

local PhysicsService = game:GetService("PhysicsService")

local startPoint  = Vector3.new(0, 0, 0)
local direction  = Vector3.new(0, 0, -3.2)
local ray = Ray.new(startPoint, direction)

local hit, hitPosition, hitNormal = PhysicsService:Intersect(ray)

if hit then
	print("Hit position:", hitPosition)
	print("Hit normal:", hitNormal)
end

I would also like to excluce objects with certain names from the raycast detection, so if someone knows how to do that, it would help me a lot too.

This is my first post and I tried to be as clear as possible. I would be so happy if someone could help me :slight_smile:

Yeah, it doesn’t work because it’s not valid code (and probably never was). PhysicsService.Intersect never existed in the first place.

What you’re looking for are Raycasts, not Rays.

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.FilterDescendantsInstances = {workspace.Model}
raycastParams.IgnoreWater = true

local part = workspace.Part
local raycastResult = workspace:Raycast(part.Position, part.CFrame.LookVector * 50, raycastParams)

if raycastResult then
	print("Object/terrain hit:", raycastResult.Instance:GetFullName())
	print("Hit position:", raycastResult.Position)
	print("Surface normal at the point of intersection:", raycastResult.Normal)
	print("Material hit:", raycastResult.Material.Name)
else
	print("Nothing was hit!")
end

I suggest you also take a look at RaycastParams, which can be used to modify the behavior of your Ray cast.

Oh, thank you very much! I’m sorry for not knowing it was not a valid code, I saw a post on the forum and copied what that code said. Sorry for asking dumb questions!

I didn’t mean to offend you. If anything, whoever wrote this code is to blame.

It worked! Thank you very muchh!!
PD: don’t worry, you didn’t offended me