I'm trying to make the brick change color but I keep getting a error from line 7. Error:
Workspace.Script:7: attempt to index local 'hit' (a nil value)
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
Why and how would I fix it?

6 comments
Make sure
hitexists.It does when it's found by the ray
But if it isn't? Do something like repeat wait() until hit ~= nil and then check if hit:IsA("Part")
I'm really confused what you mean Goulstem because i'm trying to find what the ray hit and if I do repeat until hit~=nil it still won't be able to find the part.
Ok,I fixed it, I did what Goulstem said and did a if hit == nil then
What I'm saying is to wait until the hit exists. It's very likely that the script processed before the ray did. So wait until the hit exists and then procede with the script.(: