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

2 comments
Perhaps try print(hit.Name)?
I got an error saying:Workspace.Script:7: attempt to index local 'hit' (a nil value)