Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help with raycasting? - ScreenPointToRay

Essentially, I've created a loadout system which creates a character model and puts it into a message inside the character (so it is local to the player). Problem is, I want my script to detect if the player is right clicking on the avatar. My first thought was using Mouse.Target (the easy option), however due to the avatar being a descendant of the character, the avatar is automatically filtered from the Mouse.Target.

I then considered creating a second avatar and welding it to the first (and making it transparent) however it would no longer be local to the player (and it needs to be destroyed when the player respawns).

Finally, I thought of raycasting and created the following statement to locate the part:

local Target = workspace:FindPartOnRay(Camera:ScreenPointToRay(Mouse.X, Mouse.Y, 100))

However wherever my mouse is, Target always reads nil...

I created a part and sized/positioned it to track the ray. The part went through the entire map and ended 100 studs away (at the depth I inserted). So the ray decided to go through everything, despite there being no ignore...

(this is wrapped in a Button2Down event which is definitely running through this)

There are no errors.

Any ideas?

2 answers

Perhaps placing objects in the Character makes it so raycasting can't target them either? Myself, I'd probably just overlay an invisible button wherever you place the avatar on the screen, and read events from that.

1 comment

0

Problem is, when screensizes change, so will the boxes... Also the raycast isn't reading ANYTHING. Not even the baseplate

I managed to work out a bypass. I created a transparent, non-cancollided box around the character and used Mouse.Target on it. It's not ideal, but it works.

This discussion is preserved as it was. New answers, comments, and votes are closed.