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

How to get the part that a ray hits?

Asked by 4 years ago
Edited 4 years ago
local handle = script.Parent.Handle

script.Parent.Activated:Connect(function()
    local plr = game.Players.LocalPlayer
    local mouse = plr:GetMouse()
    local target = mouse.Target
    if target.Name == "Well Water" then
        local fishray = Ray.new(handle.Position,(handle.CFrame.p - mouse.Hit.p).unit * 100)
        local part, position = workspace:FindPartOnRay(fishray, plr.Character)
        if part and part.Name == ("Well Water") then
            print "success"
        end
    end
end)

1 answer

Log in to vote
1
Answered by
palav 104
4 years ago

That most likely means a part isn't being found.

For your if command, do

if part and part.Parent and Part.Name == "Well Water" then
    --code
end
0
So how do I find the part? I'll post the basic script. Edbotikx 99 — 4y
0
You find it by using FindPartOnRay. A ray won't always find a part, if you fire it towards the sky, it will return nil. palav 104 — 4y
Ad

Answer this question