Raycast randomly offsets

i am trying to create a part that players can throw, i create it locally for each player to remove the weird “delay” whenever a new part is being created, and check whether it has passed a collideable surface with raycasts so i know when to stop it’s movement

However, the last raycast, aka the one that hits a wall seems to act weird, activating before actually hitting the wall, being randomly offset

I went thru some Dev Forums and asked ChatGPT for answers, none of these worked tho

My code:

AbilityModuleScript:
(The createBeam and CreatePart are some functions made by ChatGPT for visualization, everything else is mine(i removed the functions themselfs to make the code as compact as possible))

function AbilityModule.InkBomb()
	
	local Player = game.Players.LocalPlayer
	
	local InkBombEvent = game.ReplicatedStorage.Events.InkBombEvent
	
	local RunService = game:GetService("RunService")
	local CollectionService = game:GetService("CollectionService")
	
	local CoreModule = require(game.ReplicatedStorage.Modules.CoreModuleScript)
	
	InkBombEvent:Fire(Player)
	
	local InkBombClone = nil
	
	while not InkBombClone do
		
		for i, InkBomb in pairs(CollectionService:GetTagged("InkBomb")) do

			if InkBomb:GetAttribute(tostring(Player)) == false then

				InkBombClone = InkBomb
				InkBomb:SetAttribute(tostring(Player), true)

				break

			end
		end
		
		task.wait()
		
	end
	
	local PreviousePosition = InkBombClone.Center.Position
	
	local HeartbeatConnection
	HeartbeatConnection = RunService.Heartbeat:Connect(function()
		
		if InkBombClone then
			
			local CurrentPosition = InkBombClone.Center.Position

			local RaycastResult = CoreModule.CreateRaycast(PreviousePosition, CurrentPosition, Player.Character, "Exclude")

			createBeam(PreviousePosition, CurrentPosition)

			if RaycastResult and RaycastResult.Position then

				HeartbeatConnection:Disconnect()

				InkBombClone.Center.Anchored = true
				InkBombClone.Center:PivotTo(CFrame.new(CurrentPosition))

				print(RaycastResult.Instance.Name)
				createBeam(CurrentPosition, RaycastResult.Position)

				CreatePart(CurrentPosition)
				CreatePart(PreviousePosition)
				CreatePart(RaycastResult.Position)

			end

			PreviousePosition = CurrentPosition

		else
			
			HeartbeatConnection:Disconnect()

		end
			
	end)
		
		
end

used function in CoreModuleScript:

function CoreModule.CreateRaycast(StartPosition: Vector3 | CFrame, EndPosition: Vector3 | CFrame | number, FilterDescendants: any, FilterType: "Exclude" | "Include", CollisionGroup: string, IgnoreWater: boolean, RespectCollision: boolean, BruteForceSlow: boolean)
	
	if typeof(EndPosition) == "number" then EndPosition = StartPosition.LookVector * EndPosition end
	if typeof(EndPosition) == "CFrame" then StartPosition = StartPosition.Position end
	if typeof(EndPosition) == "CFrame" then EndPosition = EndPosition.Position end
	
	if not FilterDescendants then FilterDescendants = game.Workspace end
	if not FilterType then FilterType = "Include" end
	
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = {FilterDescendants}
	RaycastParameters.FilterType = Enum.RaycastFilterType[FilterType]
	if CollisionGroup then RaycastParameters.CollisionGroup = CollisionGroup end
	if IgnoreWater ~= nil then RaycastParameters.IgnoreWater = IgnoreWater end
	if RespectCollision ~= nil then RaycastParameters.RespectCollision = RespectCollision end
	if BruteForceSlow ~= nil then RaycastParameters.BruteForceSlow = BruteForceSlow end
	
	local RaycastResult = game.Workspace:Raycast(StartPosition, EndPosition, RaycastParameters)
	
	return RaycastResult
	
end

Each beam is the distance that the ball passed each frame, the 3 highlighted circles are PreviousePos, CurrentPos and RaycastResult.Position(the one that seems to be bugged)

Aditionally it seems as it is the raycast that is hitting the parts in a weird way, rhather then something else offseting the finaly position

P.S. The model of the ball has CanCallide, CanQuery and CanTouch set to false

My first post on here so sorry if i did sum wrong

Update: it seems like i messed up, should’ve used Direction instead of Position in the second param of Workspace:Raycast(), never the less still acts a lil weird

Update: Weirdly enough, changing the PreviousePos and CurrentPos places in the CoreModule.Raycast worked a lot better (i messed up (Pos1 - Pos2).Unit code there, mb)

i will still try to find a way to make the raycast mroe reliable cuz rn around 5% of the time it doesn’t work at all