How to make player always face mouse with Raycast

Hello everyone, I have a script that makes the player always face the mouse.

game.ReplicatedStorage.Play.Event:Connect(function()
	local character = Player.Character or Player.CharacterAdded:Wait()
	while character:FindFirstChild("Humanoid").Health > 0 do
		game:GetService("RunService").Stepped:Wait()
		character.UpperTorso.CFrame = CFrame.new(character.UpperTorso.Position, Vector3.new(mouse.Hit.p.X, character.UpperTorso.Position.Y, mouse.Hit.p.Z))
	end
end)

However, I have a problem: Transparent parts, such as hitboxes can interfere with this causing inconsistencies. Someone told me to use Raycasts. I know I can blacklist certain objects, but i’m still not sure how to go about trying to use raycasts to make the player face the mouse. All help is appreciated :slight_smile:

So what you’ll have to do is constantly making Rays that start at the
A) Player’s Character
B) Player’s Camera
and go to the mouse. You will have to blacklist the invisible parts from here. You will get the result of the raycast and position the character so that is faces the resulting position.

Sorry, how do I do that? This is my second time using rays…

Are you trying to raycast from the camera or the character?

I honestly don’t know, this person told me to use rays:

Well your origin and destination of raycast all depend on what you are trying to do.

I want the player to always face the mouse, I will send a video real quick

robloxapp-20220727-1102151.wmv (1.1 MB) edit: the mouse isn’t showing for some reason, i’ll send another one

robloxapp-20220727-1104594.wmv (1.1 MB)
Edit: Weird, my mouse doesn’t show in the roblox recording. Join this game, move your mouse, and you’ll see… Wilderness Survival (Work in Progress) - Roblox

I am not extremely familiar with such but I believe you can utilize Camera:ScreenPointToRay