Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I get the part that a ray hits?

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)

6 comments

0

alright, lemme get code stuff ready

0

Line 11. use print("success")

0

hmm that didn't work, lemme try something else

0

Make sure to check if the Ray anything, otherwise it will return nil.

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

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.

1 answer

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 :) .

3 comments

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.

0

Huh? It worked for me. Weird

0

Oh, i found what i missed. Sorry, editing answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.