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

How to cast a Ray in front of character?

Prints nil and I'm surrounded by parts.

local char = workspace:WaitForChild("papasherms")
local newRay = Ray.new(
    char:GetPrimaryPartCFrame().p, 
    (char:GetPrimaryPartCFrame().p - (char:GetPrimaryPartCFrame() * CFrame.new(char.PrimaryPart.Size.X/2,0,0)).p).unit * 500
)
    
spawn(function()
    while (wait(1)) do
        local hit, pos, normal = workspace:FindPartOnRay(newRay, char)
        print(hit)
    end    
end)

1 answer

✓ Accepted answer

I think you should use a LocalScript, put a LocalScript inside StarterPlayer > StarterCharacterScripts, and paste this:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character

spawn(function()
	while wait(1) do
--tuple FindPartOnRay( Ray ray, Instance ignoreDescendantsInstance, bool terrainCellsAreCubes, bool ignoreWater ) 
		local Hit, Pos, Normal = workspace:FindPartOnRay(
			Ray.new(
				Character.HumanoidRootPart.Position,
				Character.HumanoidRootPart.CFrame.lookVector * 500
			),
			
			Character
		)
		
		print(Hit, Pos, Normal)
	end
end)

Now, please read the script carefully and try to understand what's going on, this might help you in the future. Hope this helped!

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