Detecting the boder of a wall

Ok so i have this script that detectes the border of any part, is this the best way to do it, and could it cause performance issues? im doing this for testing

If you want to try it for yourself, you copy this as a local script and place it at StarterCharacterScripts, you need to touch a part while being in the air with the tag “CanClimb”

if you want to detect a really long wall you need to set “MaxLoopCounter” to a big number like 1000 or more

if it detects a border it will instance a part at the border, it will also say it in the console


local UserInputService = game:GetService("UserInputService")
local Debris = game:GetService("Debris")

local Collection = game:GetService("CollectionService")
local TagName = "CanClimb"

local Character = script.Parent
local RootPart = Character.HumanoidRootPart

-- the player should be in the air while touching a part, to actually send the raycast the JumpRequest event
-- needs to fire (the HitPart needs to have the tag "CanClimb")

Character.HumanoidRootPart.Touched:Connect(function(HitPart)

	if Character.Humanoid.FloorMaterial == Enum.Material.Air and Collection:HasTag(HitPart,TagName) then						

		local JumpConnection			
		JumpConnection = UserInputService.JumpRequest:Connect(function() -- Fires when the player tries to jump

			local RootPartPosition = RootPart.Position 
			local RootPartLookVector = RootPart.CFrame.LookVector  

			-- The scripts sends the first raycast before finding the border, its purpose its to find out 
			-- if the player its facing at the part he touched, if the raycast hits something 
			-- (wich means the player its facing at the part) it start finding the corner

			local Length = 2 -- Raycast Lenght
			local FirstRaycast = workspace:Raycast(RootPartPosition, RootPartLookVector * Length)

			local Counter = 0

			repeat 
				Counter += 1 					
				if Counter > 500 then -- The raycast hasn't found anything
					return
				end						
			until FirstRaycast -- The raycast hit something


			-- FINDING THE BORDER
			------------------------------


			local MaxLoopCounter = 100
			local LoopCounter = 0

			local RaycastTravelTime = 0
			local LastRaycastPosition 

			-- Everytime the loop runs its gonna launch a raycast, but we are gonna increase the y-axis by 0.1 every iteration
			-- if the raycast hits something that means that the raycast its still hitting the wall
			-- but if it hits nothing it means that the corner was found


			while LoopCounter < MaxLoopCounter do

				local RaycastCorner = workspace:Raycast(RootPartPosition + Vector3.new(0, 1+LoopCounter ,0) , RootPartLookVector * 4 + Vector3.new(0, 1+LoopCounter ,0)  )
				LoopCounter += 0.1

				repeat 			
					RaycastTravelTime += 1									
					if RaycastTravelTime > 500 then 
						break
					end									
				until RaycastCorner	


				if not RaycastCorner then	 
					InstancePart(LastRaycastPosition, BrickColor.new("Baby blue"))
					warn("Corner was found!!!")											
					break
				else						
					LastRaycastPosition = RaycastCorner.Position   
					-- we save the raycast position so we can instance a part at that position when the raycast is found 
					-- (we cannot use the position of a raycast that hasnt hit anything)
				end				

			end							
			JumpConnection:Disconnect()	
		end)
	end
end)




function InstancePart(Position, Color)
	local Part = Instance.new("Part", workspace)
	Part.Anchored = true
	Part.CanCollide = false
	Part.Position = Position
	Part.BrickColor = Color
	Part.Size = Vector3.new(.5, .5, .5)

	Debris:addItem(Part,1)
end