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

How could I make this Raycasting script keep getting the currently hit part?

Hi, so I am trying to make it so everytime it hits a new part it will print it, but currently it only prints the first part it hit then stops. I've tried converting it from if hitPart to while hitPart do, that didn't work. Please help, all advice is appreciated. Thank you!

local car = script.Parent.Car.Value
local seat = car.DriveSeat
local BSM = car.Body.BSM

local rayOriginL = BSM.LBSM.Position
local rayDirectionL = BSM.LBSM.CFrame.LookVector * 20

local ray = Ray.new(rayOriginL, rayDirectionL)

local hitPart, hitPosition = workspace:FindPartOnRay(ray)


if hitPart then
	print("Hit part: " .. hitPart:GetFullName())
else
	print("Did not hit part")
end

1 answer

✓ Accepted answer

This is pretty easy to do.

All you need is a while loop.

This here should work:

local car = script.Parent.Car.Value
local seat = car.DriveSeat
local BSM = car.Body.BSM

local rayOriginL = BSM.LBSM.Position
local rayDirectionL = BSM.LBSM.CFrame.LookVector * 20


while wait() do
	local ray = Ray.new(rayOriginL, rayDirectionL)
	
	local hitPart, hitPosition = workspace:FindPartOnRay(ray)
	
	
	if hitPart then
	    print("Hit part: " .. hitPart:GetFullName())
	else
	    print("Did not hit part")
	end
end

2 comments

1

Thanks!

0

No problem!

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