Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with rays?

Asked by 10 years ago

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?

1
Make sure `hit` exists. l0cky2013 135 — 10y
0
It does when it's found by the ray kevinnight45 550 — 10y
1
But if it isn't? Do something like repeat wait() until hit ~= nil and then check if hit:IsA("Part") Goulstem 8144 — 10y
0
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. kevinnight45 550 — 10y
View all comments (2 more)
0
Ok,I fixed it, I did what Goulstem said and did a if hit == nil then kevinnight45 550 — 10y
1
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.(: Goulstem 8144 — 10y

1 answer

Log in to vote
1
Answered by 10 years ago

I do not have a solution to your problem, though I do know the reason that it does not work.

Your problem is that you are trying to check if hit is a part before it actually exists. That is due to the while loop. Basically, you are constantly checking if hit is a part, before the ray actually hits anything.

Now, I have a theory as to how you could solve your problem, although I am not sure how it would work. If rays can fire functions when they touch something, then you could have it run a function changing the BrickColor of the hit object if it is a part every time it hits something. I am not sure how that would work, as I am no expert with rays, but I am sure it is possible.

What Goulstem was getting at:

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)
    repeat wait() until hit~=nil --Waits until hit is not nil.
    if hit:IsA("Part") then
    game.Workspace.Part1.BrickColor = BrickColor.new("Bright green")
else
    game.Workspace.Part1.BrickColor = BrickColor.new("Bright red")
end
end

Hope I helped!

0
I'm pretty sure rays don't work as events and fire stuff,but thanks kevinnight45 550 — 10y
1
It didn't hit nil, the output is telling you that hit is equivalent to nil, meaning thath it doesn't exist because it hasn't hit anything. SlickPwner 534 — 10y
0
Sorry I edited it kevinnight45 550 — 10y
1
I edited my answer to include was Goulstem was getting at. SlickPwner 534 — 10y
Ad

Answer this question