Multiple raycast from same origin?

Hello, im currently trying to create a LiDAR Gun similar to Scanner Sombre. I have the script in a tool and it does work, but it only creates one ball at the result position! It also places one ball per click and i would like to make it continuously scan when the player is pressing mousebutton1 if that makes sense.

ive tried adding this to the code but it will then put 20 parts at the same result position

maxRay = 20
for i = 1, maxRay do

my question is, how can i make multiple raycasts shoot from the same origin but place around eachother instead of together? I want the players to be able to scan the walls, roof, and floor to find the path out (all scannable parts will be under the same name for params purposes unless theres an easier way)

here is what i have so far;

local player = game:GetService(“Players”).LocalPlayer

local input = game:GetService(“UserInputService”)

local lidarGun = script.Parent
local handle = lidarGun.Handle

local folder = Instance.new(“Folder”, workspace)
folder.Name = “RayParts”

input.InputBegan:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print(“mouse clicked”)
local rayOrigin = handle.Position
local rayDirection = handle.CFrame.LookVector * 100 – 100 studs forward
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Include
raycastParams.FilterDescendantsInstances = {workspace.Wall}

	local result = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

	if result then
		print("Hit: ", result.Instance:GetFullName())
		print("Position: ", result.Position)
		print("Distance: ", (result.Position - rayOrigin).Magnitude)

		local rayPart = Instance.new("Part")
		rayPart.Shape = Enum.PartType.Ball
		rayPart.Position = result.Position
		rayPart.Size = Vector3.new(0.1, 0.1, 0.1)
		rayPart.Anchored = true
		rayPart.CanCollide = false
		rayPart.BrickColor = BrickColor.new("Bright red")
		rayPart.Material = Enum.Material.Neon
		rayPart.Reflectance = .5
		rayPart.Parent = folder
		task.wait()
	else
		print("No hit detected.")
	end
end

end)

you’re creating the same connection 20 times because of the for loop, just run the for loop once LMB is clicked

if you want automatic shooting, you can do:

local uInputService = game:GetService("UserInputService")

while uInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) do
 -- code logic here
end

ahh okay that makes sense thank you!! as for the raycast hitting in one spot, should i use a vector3 with a math.random? im still new to raycasting so the multiple casts in a cone shape in throwing me for a loop.

uhhhh i’ve never really applied random offsets to raycasts, so idk

1 Like

all good, i appreciate the help with the loop!