Unable to find character head and player mouse

  1. What do you want to achieve?
    I want to find the player’s head and mouse positions to cast a raycast from the head position to the mouse position

  2. What is the issue?
    It will not cast a ray

  3. What solutions have you tried so far?
    I have tried rewriting the script, adding waits, and following the dev hub post

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
------------------------------------------------

local rayOrigin = player:WaitForChild("Character", 12).Head.Position
local rayDirection = mouse.Hit.Position

local rayp = RaycastParams.new()
rayp.FilterDescendantsInstances = {workspace.Boxes}
rayp.FilterType = Enum.RaycastFilterType.Include

local function performRaycast(rayOrigin, rayDirection)

	local rayResult = workspace:Raycast(rayOrigin, rayDirection)

	if rayResult then
		print("Instance:", rayResult.Instance)
		print("Position:", rayResult.Position)
		print("Distance:", rayResult.Distance)
		print("Material:", rayResult.Material)
		print("Normal:", rayResult.Normal)
	else
		warn("No raycast result!")
	end
end




mouse.Button1Up:Connect(performRaycast)

I think you are not using a (direction vector) for rayDirection, but rather you are using an actual world position.

Maybe CFrame.lookAt(rayOrigin,mouse.Hit.Position).lookVector ?

Or something like that

That certainly could fix part of the problem, although it doesn’t seem to be able to find the player’s character or mouse so I’m unsure if it will help

Are you running this in a local script under the character?

I don’t think you can do a player:WaitForChild(“Character”)
You need to do something like
while not player.Character do wait() end

or if this script is under starter character scripts, you can do
character = script.Parent

There’s a few issues with the script so I’ll write them out here:

  1. You’re using player:WaitForChild("Character") and Character is not a descendant of player, rather it’s a property, you can only wait for Instances that are Parented to your Instance, in this case: player. To combat this you can check if the player exists and if it doesn’t you wait for it to load.
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
-- If the first thing is nil then it goes to the second and returns that
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
  1. Your Ray Direction is wrong, if you want to get a vector that points from the characters head to the Mouse.Hit.Position then you would use Vector math, more specifically subtracting Vectors. Here is a visual for vector subtraction:

As you can see if you have two vectors, one that points to Position A and one that points to Position B then if you subtract Vector A from Vector B you will get a vector that when placed at the tip of Vector B it will go to the tip of Vector A, thus a Direction Vector.

So in our case we want to get the distance from Character.Head.Position to Mouse.Hit.Position in the direction of the mouse, which would be
A - B = C : Mouse.Hit.Position - Character.Head.Position = raycastDirection

So the code would look like this:

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local Mouse = localPlayer:GetMouse()
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

local directionVector = Mouse.Hit.Position - character.Head.Position

So the final code would look like:

local Players = game:GetService("Players")
local localPlayer = players.LocalPlayer
local Mouse = player:GetMouse()
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

local rayOrigin = Character.Head.Position
local rayDirection = Mouse.Hit.Position - rayOrigin

local rayp = RaycastParams.new()
rayp.FilterDescendantsInstances = {workspace.Boxes}
rayp.FilterType = Enum.RaycastFilterType.Include

local function performRaycast(rayOrigin, rayDirection)

	local rayResult = workspace:Raycast(rayOrigin, rayDirection)

	if rayResult then
		print("Instance:", rayResult.Instance)
		print("Position:", rayResult.Position)
		print("Distance:", rayResult.Distance)
		print("Material:", rayResult.Material)
		print("Normal:", rayResult.Normal)
	else
		warn("No raycast result!")
	end
end




mouse.Button1Up:Connect(performRaycast)

Just note that the code will barely function since you’re getting Mouse.Hit.Position right upon initialization of the script.

I moved the checks for the head into the button detection code
used a cframe for the direction of the ray
and check for character without a waitforchild

Seems to work

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
------------------------------------------------

while not player.Character do task.wait() end



local rayp = RaycastParams.new()
rayp.FilterDescendantsInstances = {workspace.Boxes}
rayp.FilterType = Enum.RaycastFilterType.Include

local function performRaycast(rayOrigin, rayDirection)
	
	if mouse.Hit then
		local rayOrigin = player.Character:WaitForChild("Head").Position
		local rayDirection =  CFrame.lookAt(rayOrigin,mouse.Hit.Position).LookVector
		
		local rayResult = workspace:Raycast(rayOrigin, rayDirection)

		if rayResult then
			print("Instance:", rayResult.Instance)
			print("Position:", rayResult.Position)
			print("Distance:", rayResult.Distance)
			print("Material:", rayResult.Material)
			print("Normal:", rayResult.Normal)
		else
			warn("No raycast result!")
		end 
	else
		warn("No mouse.Hit ")
	end
end




mouse.Button1Up:Connect(performRaycast)

LookVector is a unit vector so you would have to get the distance from mouse.Hit.Position and rayOrigin and multiply that by the lookVector to get the proper result

yes, correct.

so, add this

		local rayOrigin = player.Character:WaitForChild("Head").Position
		local ray = mouse.Hit.Position - rayOrigin
		local rayDirection = ray.Unit  * (ray.Magnitude + 1)

Sorry for the late reply to the both of you, I had school and other things get in the way so i wasn’t at my computer.

Anyways thanks for helping, otherwise I wouldn’t have figured out any of those problems! I still have one problem where it keeps detecting my hats despite the raycast parameters, I also have another issue with the rest of the script (which I removed before making the devforum post) however I’m unsure if I should make a new post or continue under this one.