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

How do I get the part that a ray hits?

Asked by 5 years ago
Edited 5 years ago

The problem is on line 10, when I try to get the name of the part. Output says that part is a nil value.

01local tool = script.Parent
02local player = game.Players.LocalPlayer
03local mouse = player:GetMouse()
04 
05 
06script.Parent.Activated:connect(function()
07        if mouse.Target.Name == "Water"  then
08            local ray = Ray.new(tool.Handle.Position, mouse.Target.Position)
09            local part, point = workspace:FindPartOnRay(ray)
10            if part.Name == "Water" then
11                print "success"
12            end
13        end
14end)
0
alright, lemme get code stuff ready BashGuy10 384 — 5y
0
Line 11. use print("success") BashGuy10 384 — 5y
0
hmm that didn't work, lemme try something else BashGuy10 384 — 5y
0
Make sure to check if the Ray anything, otherwise it will return nil. TheeDeathCaster 2368 — 5y
View all comments (2 more)
0
Btw, for your script, it's taking the Target position and multiplying by 300. Say the vector3 was 1,1,1, it will become 300,300,300. That's a really big different. The Ray bit should be (End - Start).Unit * 300 TheeDeathCaster 2368 — 5y
0
"Make sure to check if the Ray anything, otherwise it will return nil." What does that mean? Also, I got rid of the 300 thing. I want to put a limit on how far it would go, but I can do that after I get the part. Edbotikx 99 — 5y

1 answer

Log in to vote
-1
Answered by
BashGuy10 384 Moderation Voter
5 years ago
Edited 5 years ago

Alright, here ye go. some help.

It simple to fix this problem.

Use:

01local tool = script.Parent
02local player = game.Players.LocalPlayer
03 
04 
05 
06tool.Equipped:Connect(function(mouse)
07 
08 
09    mouse.Button1Down:Connect(function()
10        local ray = Ray.new(tool.Handle.Position, mouse.Target.Position)
11        local part, point = workspace:FindPartOnRay(ray)
12        if part.Name == "Water" then
13            print "success"
14        end
15    end)
16 
17end)

I forgot somethings sorry, here have a smiley face :) .

0
That didn't work. It still says "13: attempt to index local 'part' (a nil value)" when I try to get 'part's name. Edbotikx 99 — 5y
0
Huh? It worked for me. Weird BashGuy10 384 — 5y
0
Oh, i found what i missed. Sorry, editing answer. BashGuy10 384 — 5y
Ad

Answer this question