Moving on the ledge of a part

I wanted to make a ledge grab system, where you can move only left or right on the ledge of the wall. I used raycasts to find a part with two attachments that are placed at the start and at the end of the wall. Basically after you press space it teleports you on the ledge and i used body velocity to make it so the player doesn’t fall. For moving left or right i just changed body velocity settings.

The issue is that i can’t make it stop moving when it reaches one of the two attachment positions

I tried using a while loop that check constantly the player’s position, but it doesn’t work at all:

--// bool would be if the player is holding down the button
function LedgeGrabModule:MoveRight(bool)
	local player = LedgeGrabTable.player
	local Velocity = player.Character.HumanoidRootPart.Attachment.LinearVelocity
	if bool == true then
		while LedgeGrabTable.wall.LedgeGrab2.WorldCFrame.Position.X >= player.Character.HumanoidRootPart.CFrame.Position.X or
			LedgeGrabTable.wall.LedgeGrab1.WorldCFrame.Position.X <= player.Character.HumanoidRootPart.CFrame.Position.X do
			Velocity.VectorVelocity = Vector3.new(0, 0, 0)
			task.wait(0.01)
		end
		
		if LedgeGrabTable.wall.LedgeGrab2.WorldCFrame.Position.X < player.Character.HumanoidRootPart.CFrame.Position.X and 
			LedgeGrabTable.wall.LedgeGrab1.WorldCFrame.Position.X > player.Character.HumanoidRootPart.CFrame.Position.X then
			Velocity.VectorVelocity = Vector3.new(-5, 0, 0)
		end
	else
		Velocity.VectorVelocity = Vector3.new(0, 0, 0)
	end
end

If you managed to fix it, could you please explain what you did so i can understand? Thank you

--// bool would be if the player is holding down the button
function LedgeGrabModule:MoveRight(bool)
	local player = LedgeGrabTable.player
	local Velocity = player.Character.HumanoidRootPart.Attachment.LinearVelocity
	if bool == true then
		while LedgeGrabTable.wall.LedgeGrab2.WorldCFrame.Position.X >= player.Character.HumanoidRootPart.CFrame.Position.X or
			LedgeGrabTable.wall.LedgeGrab1.WorldCFrame.Position.X <= player.Character.HumanoidRootPart.CFrame.Position.X do
			Velocity.VectorVelocity = Vector3.new(0, 0, 0)
			player.Character.HumanoidRootPart.Velocity = Vector3.zero
			task.wait(0.01)
		end

		if LedgeGrabTable.wall.LedgeGrab2.WorldCFrame.Position.X < player.Character.HumanoidRootPart.CFrame.Position.X and 
			LedgeGrabTable.wall.LedgeGrab1.WorldCFrame.Position.X > player.Character.HumanoidRootPart.CFrame.Position.X then
			Velocity.VectorVelocity = Vector3.new(-5, 0, 0)
		end
	else
		Velocity.VectorVelocity = Vector3.new(0, 0, 0)
		player.Character.HumanoidRootPart.Velocity = Vector3.zero
	end
end

maybe also update the humanoid actually velocity? iudk if this works

if my first reply doesnt work maybe this will

--// bool would be if the player is holding down the button
function LedgeGrabModule:MoveRight(bool)
	local player = LedgeGrabTable.player
	local Velocity = player.Character.HumanoidRootPart.Attachment.LinearVelocity
	if bool == true then
		while LedgeGrabTable.wall.LedgeGrab2.WorldCFrame.Position.X >= player.Character.HumanoidRootPart.CFrame.Position.X or
			LedgeGrabTable.wall.LedgeGrab1.WorldCFrame.Position.X <= player.Character.HumanoidRootPart.CFrame.Position.X do
			Velocity.VectorVelocity = Vector3.new(0, 0, 0)
			player.Character.HumanoidRootPart:ApplyImpulse(Vector3.zero)
			task.wait(0.01)
		end

		if LedgeGrabTable.wall.LedgeGrab2.WorldCFrame.Position.X < player.Character.HumanoidRootPart.CFrame.Position.X and 
			LedgeGrabTable.wall.LedgeGrab1.WorldCFrame.Position.X > player.Character.HumanoidRootPart.CFrame.Position.X then
			Velocity.VectorVelocity = Vector3.new(-5, 0, 0)
		end
	else
		Velocity.VectorVelocity = Vector3.new(0, 0, 0)
		player.Character.HumanoidRootPart:ApplyImpulse(Vector3.zero)
	end
end

Thank you for your reply, but i found another problem:
basically right now by holding down the button, when the character arrives to the point it should stop, it doesn’t, but it’ll work only if the character’s position is already past that point.

sorry for the late reply are you still having this problem?

yeah i tried to change some stuff but still doesn’t work