Bullet projectiles not spawning in the right place

So, I have been following a YouTube tutorial on how to get smooth projectile bullets that do not cause lag. It works great, however an issue that is happening to me is that the projectile is not spawning in the right place when the player is walking or jumping, however it is normal when standing.

It looks bad and this is kind of a big deal so if you know how to prevent this from happening or making it less obvious, Iā€™m all ears. :smiley:

Edit: example of the issue:
https://gyazo.com/cb7e9a22bff680934e2805dfb1f8c822

Code that replicates the bullet on the client:

local bulletStorage = game.Workspace.BulletStorage

function fireF(character,startPos,direction,range,gravity, tool)
	local lastPos = startPos
	local startTime = tick()
	
	local bullet = game.ReplicatedStorage.Apollo.Bullet:Clone()
	bullet.Size = Vector3.new(0.001,0.001,0.001)
	bullet.Anchored = true
	bullet.CanCollide = false
	bullet.BrickColor = BrickColor.new("Burlap")
	bullet.Material = Enum.Material.Neon	
	bullet.Position = startPos

	bullet.Parent = bulletStorage
	
	local rayFilter = RaycastParams.new()
	rayFilter.FilterDescendantsInstances = {character,bulletStorage}
	rayFilter.FilterType = Enum.RaycastFilterType.Blacklist

	while range > 0 do
		local timeLength = (tick()-startTime)

		local currentPos = startPos + (direction*timeLength)
		currentPos += (gravity*timeLength^2)
		
		local distance = (lastPos - currentPos).Magnitude
		
		range -= distance
		
		local ray = workspace:Raycast(lastPos,currentPos-lastPos,rayFilter)
		if ray == nil then
			ray = workspace:Raycast(currentPos,lastPos-currentPos,rayFilter)
		end

		if ray then
			local hit = ray.Instance
			currentPos = ray.Position
		end
		
		bullet.CFrame = CFrame.new(currentPos,lastPos)*CFrame.new(0, 0, -(lastPos-currentPos).magnitude/2)
		bullet.Size = Vector3.new(0.2, 0.2, (lastPos-currentPos).magnitude)

		if ray then
			break
		end
		
		lastPos = currentPos
		local t = tick()
		repeat task.wait() until tick()-t >= 1/60

	end
	local t = tick()
	repeat task.wait() until tick()-t >= 1/60
	bullet:Destroy()
end

game.ReplicatedStorage.Apollo.BulletReplication.OnClientEvent:Connect(fireF)