Issue with rays

I have a system for making a game similar to scan test, anyways here is the code

local rays = {}

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local folder = Instance.new("Folder", char)

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude

local mouse = player:GetMouse()

local RS = game:GetService("RunService")

local UIS = game:GetService("UserInputService")

local LookVectorInt = 3
local RightVectorInt = -3.5
local UpVectorInt = 0

function CreateRays()
	local humanoidRootPart = char.HumanoidRootPart
	if #rays ~= 100 then
		for i = 1, 10 do
			for n = 1, 10 do
				local newRayPart = Instance.new("Part", folder)
				newRayPart.Anchored = true
				newRayPart.Size = Vector3.new(0.1, 0.1, 0.1)
				newRayPart.Name = tostring(i) .. "_" .. tostring(n)
				newRayPart.CanCollide = false
				newRayPart.Transparency = 1
				newRayPart.CanQuery = false
				local lookVector = humanoidRootPart.CFrame.LookVector
				local rightVector = humanoidRootPart.CFrame.RightVector
				local upVector = humanoidRootPart.CFrame.UpVector

				rays[newRayPart.Name] = {
					LookVector = LookVectorInt,
					rightVector = RightVectorInt,
					upVector = -UpVectorInt
				}

				local setPosition = humanoidRootPart.Position + 
					(lookVector * LookVectorInt) + 
					(rightVector * - RightVectorInt) + 
					(upVector * - UpVectorInt)
				
				newRayPart.Position = setPosition

				RightVectorInt += 0.75
			end
			UpVectorInt -= 0.35
			RightVectorInt = - 3.5
		end
	end
end


function UpdateRays()
	for i, part in pairs(rays) do
		local humanoidRootPart = char.HumanoidRootPart
		local lookVector = humanoidRootPart.CFrame.LookVector
		local rightVector = humanoidRootPart.CFrame.RightVector
		local upVector = humanoidRootPart.CFrame.UpVector

	
		local setPosition = humanoidRootPart.Position + (lookVector * rays[i].LookVector) + (rightVector * rays[i].rightVector) + (upVector * rays[i].upVector)

		local foundPart = folder:FindFirstChild(i)
		if foundPart then
			foundPart.Position = setPosition
		end
	end
end


UIS.InputBegan:Connect(function(input)
	if input and input.KeyCode == Enum.KeyCode.E then
		for i, v in pairs(rays) do
			
			local newRay = workspace:Raycast(folder[i].Position, char.HumanoidRootPart.CFrame.LookVector * 10)
			
			if newRay then
				if newRay.Position and newRay.Instance then
					local newHitEffect = game.ReplicatedStorage.RayHit:Clone()
					newHitEffect.Parent = workspace.HitEffects
					newHitEffect.Position = newRay.Position
					newHitEffect.CanCollide = false
					
				end
			end
		end
	end
end)

local RayCastingMouse = false

function MouseRayCast()
	while task.wait(0.01) do
		if RayCastingMouse == true then
			local HeightIncrease = 0
			local SideIncrease = 0

		
				for n = 1, 20 do
					local Camera = game:GetService("Workspace").CurrentCamera
					local mousePosition = mouse.Hit.Position + Vector3.new(HeightIncrease, SideIncrease, 0)
					if (mouse.Hit.Position - char.HumanoidRootPart.Position).Magnitude >= 20 then continue end
					local rayOrigin = Camera.CFrame.Position
					local rayDirection = (mousePosition - rayOrigin).unit * 1000

					local newRay = workspace:Raycast(rayOrigin, rayDirection, rayParams)

					if newRay then
						if newRay.Position and newRay.Instance then
							local newHitEffect = game.ReplicatedStorage.RayHit:Clone()
							newHitEffect.Parent = workspace.HitEffects
							newHitEffect.Position = newRay.Position
							newHitEffect.CanCollide = false
							newHitEffect.Anchored = true
							if newRay.Instance:IsDescendantOf(game.Workspace.AI) then
								newHitEffect.BrickColor = BrickColor.new("Bright red")
							end
						end
					end

					SideIncrease += math.random(-10, 10)/7
					HeightIncrease += math.random(-10 , 10)/7
			end
		end
	end
end


mouse.Button1Down:Connect(function()
	RayCastingMouse = true
end)

mouse.Button1Up:Connect(function()
	RayCastingMouse = false
end)

CreateRays()

rayParams.FilterDescendantsInstances = {folder, char, workspace.HitEffects}

player.CharacterAdded:Connect(function(newChar)
	char = newChar
	rayParams.FilterDescendantsInstances = {folder, char, workspace.HitEffects}
end)

task.wait(1)

task.spawn(MouseRayCast)
RS.RenderStepped:Connect(UpdateRays)


As you can see in the video the first wall I hold to scan on the first fall it looks nice, but one the second one its randomized in just straight lines which is not the desired effect, i would like to know how to make the effect be like such on the first wall on all walls

2 Likes