Raycast part isn't resizing correctly

Sometimes it overshoots and goes through the part that is being aimed at, and sometimes it undershoots and does not reach the part. Why is this happening and how can I fix it? Here’s my script:

--Determines the size of the part based on two Vector3s.
local function setBeamLength(beam, hit)
	local distance = (((beam.Position) - hit).Magnitude)
	print(distance)
	beam.Size = Vector3.new(distance, 1, 1)
end

--Creates the beam iteself and offsets its position relative to the player's right arm.
local function createFastAttackBeam(plr, hit)
	local beam = replicatedStorage:FindFirstChild("Lightning"):Clone()
	local char = plr.Character
	local rightArm = char:FindFirstChild("Right Arm")
	setBeamLength(beam, hit)
	local offset = CFrame.new(((beam.Size.X / 2) + 4), 0, 0)
	beam.CFrame = (CFrame.lookAt(rightArm.Position, hit) * CFrame.Angles(math.rad(0), math.rad(90), math.rad(0))):ToWorldSpace(offset)
	beam.Parent = game.Workspace
	createFadeBeam(beam) --Tweens the beams size/transparency out, does not effect length.
end

¿Are you using “hit” argument as your ray result Instance or Position?

It’s a position. ​​​​​​​​​​​​​​​​​​​