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

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
Dexiber 272 Moderation Voter
4 years ago

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
1
Thanks! Car00071 11 — 4y
0
No problem! Dexiber 272 — 4y
Ad

Answer this question