How can I get a ray to stop at a certain point? (SS Anti Noclip)

I’m using waiting for RunService.Heartbeat and then running this function on every player to detect noclipping and other exploits (Obviously it’s not done and doesn’t detect much yet, I’m just testing the noclip)

local function check(plr)
	local c = plr.Character
	if c then
		local h = c:FindFirstChild("Humanoid")
		local hrp = c:FindFirstChild("HumanoidRootPart")
		if h and hrp then
			local lastPos = lastPosTbl[plr] or hrp.Position
			local newPos = hrp.Position
			lastPosTbl[plr] = newPos
			local state = h:GetState()
			if blacklistedStates[state] then
				reasons.blacklistedState(plr, tostring(state))
			end

			-- check if walked through collidable part
			if not compare(lastPos, newPos) then

				local orig = lastPos
				local dir = newPos
				
				print(tostring(orig))
				print(tostring(dir))
				
				params.FilterDescendantsInstances = {c}
				params.FilterType = Enum.RaycastFilterType.Blacklist
				
				local resHoriz = workspace:Raycast(orig, dir, params)

				if resHoriz and resHoriz.Instance and resHoriz.Instance.CanCollide == true then
					reasons.noclip(plr, resHoriz.Instance.Name)
					return false
				end
			end
			--


			if flags[plr] > maxFlags then
				reasons.maxFlags(plr)
				return false
			end

			return true
		else
			flags[plr] += 1
			return false
		end
	else
		flags[plr] += 1
		return false
	end
end

The reasons.noclip function just concats some arguments and prints noclip | player's name | part they walked through. The compare function just rounds two vectors to the nearest tenth because if I was to compare the original vectors to determine if the player moved it would get false positives because of the idle animation.

The issue is that if I get close to the part, the ray lines up to trigger a false positive (read on for visual representation). Watch the output in this GIF:
https://gyazo.com/0d57ab97a0979835709ed1ee2e845d1e

I made blocks to physically represent the values of the Raycast:


These are the centers of the HumanoidRootPart positions. The white block represents the origin of the raycast (the first value printed), and the yellow represent the direction (the second value printed).

The ray is going into the part because I moved forward in that direction. I could implement a check of the root part’s position in comparison to the part’s position, but I want to make the check work with any collidable part in the game.

Does anyone know how I could do this?

TLDR: How can I get a ray to stop at a certain point?

I believe this is because you’re passing newPos as the direction instead of newPos - lastPos