Raycast not detecting my player

Hello everyone,

I’m having an issue with ray casting.

Here’s my script:

  local mouse = UserInputService:GetMouseLocation()
  local unitRay = workspace.Camera:ScreenPointToRay(mouse.X, mouse.Y)
  local rayResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * 150)

It works really well, the ray touches precisely everything I click. The problem is that it doesn’t touch / detect my player… instead it touces whatever is in front of it.

Any idea why?

Thanks in advance.

I’m not very advanced in scripting as I’m practicing but, I think you need a line that let the Raycast know you’re looking for a player. That’s my understanding of scripts at the moment. Or it doesn’t work because the ray is coming from your player, you just need to detect when you send the Raycast off?

I don’t see a line of code where you detect anything touching the ray… Can you show more?

1 Like

Hi, :Raycast takes in raycast parameters where you can identify what objects you want the raycast to detect.

1 Like

Sure can,

local partHit = rayResult.Instance
local partParent = partHit.Parent
local humanoidRootPart = partParent:FindFirstChild("HumanoidRootPart")

When I click in any player other than my player, humanoidRootPart returns the part, but when I click on my player it returns nil.

And if I debug partHit it returns whatever is in front of my player… I’m assuming the ray origin is ahead of my player position.

1 Like

Huh, I totally forgot about the new raycasting system. I think by default, your character is ignored. Such as trying to place your mouse on the player and print the hit object. It will simple print the object behind the player as if you never existed.
Seen as i need to learn this new raycasting system, I cant help you. But I have an idea.

game.Workspace:FindPartOnRayWithWhitelist(YourRayHere, {Character})

If you only want to interact with the character, this may work. But ive not tested yet.
If i come across anything, ill let you know. Good luck! :slight_smile:
EDIT: Found out the ray system I used it deprecated. So try not to rely on it in new work.

3 Likes

Spot On!

The freaking raycast ignores the caster by default.

Thanks heaps!