I'm trying to print the name of part that is raycast too,but it prints nil. Why does it print nil and how do I fix it?
How I have it setup The red part is Part1 The green part is named Part
while wait()do local Ray = Ray.new(game.Workspace.Part1.Position,--the ray will start at this part game.Workspace.Part1.CFrame.lookVector.unit)--Ray will come out of the front local hit,position = Workspace:FindPartOnRay(Ray,_) print(hit)--print name of thing that the ray hit end
For adark
while wait()do local Ray = Ray.new(game.Workspace.Part1.Position, game.Workspace.Part1.CFrame.lookVector.unit*10) -- 1000 is normally used, but you can reduce as you see fit. Not multiplying this is only 1 stud from Part1's *center* towards its lookVector. local hit = Workspace:FindPartOnRay(Ray) if hit:IsA("Part") then game.Workspace.Part1.BrickColor = BrickColor.new("Bright green") else game.Workspace.Part1.BrickColor = BrickColor.new("Bright red") end end
This is returning nil
because the length of the Ray created is 1 stud, so even if the lookVector of the red brick is directly aiming at one of the green bricks, it won't go far enough to reach it.
To fix this, multiply the second argument given to the Ray.new()
constructor by some amount:
while wait()do local Ray = Ray.new(game.Workspace.Part1.Position, game.Workspace.Part1.CFrame.lookVector.unit*1000) -- 1000 is normally used, but you can reduce as you see fit. Not multiplying this is only 1 stud from Part1's *center* towards its lookVector. local hit,position = Workspace:FindPartOnRay(Ray,_) print(hit)--print name of thing that the ray hit end