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.

local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()


script.Parent.Activated:connect(function()
        if mouse.Target.Name == "Water"  then
            local ray = Ray.new(tool.Handle.Position, mouse.Target.Position)
            local part, point = workspace:FindPartOnRay(ray)
            if part.Name == "Water" then
                print "success"
            end
        end
end)
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:

local tool = script.Parent
local player = game.Players.LocalPlayer



tool.Equipped:Connect(function(mouse)


    mouse.Button1Down:Connect(function()
        local ray = Ray.new(tool.Handle.Position, mouse.Target.Position)
        local part, point = workspace:FindPartOnRay(ray)
        if part.Name == "Water" then
            print "success"
        end
    end)

end)

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