Gun bullet trail wont size depending on the guns range (happens sometimes)

why sometimes the trail that gets created doesnt size to its respected size (aka the range). it does sometimes but also sometimes it doesnt size to it.

local tool = script.Parent
local configFold = tool:WaitForChild("Config")
local soundFold = configFold.Sounds
local valuesFold = configFold.Values

local fireRemote = configFold:WaitForChild("FireGunRE")
local reloadRemote = configFold:WaitForChild("ReloadGunRE")

local firePointAttachment = tool.Handle:WaitForChild("FirePoint")

local reloading = false
local ammo = valuesFold.Ammo.Value

fireRemote.OnServerEvent:Connect(function(plr, mousePos)
    if valuesFold.CanFire.Value and not reloading and ammo >= 1 then
        local range = valuesFold.Range.Value
        local damage = valuesFold.Damage.Value

        local firePoint = firePointAttachment.WorldPosition

        local direction = (mousePos - firePoint).Unit
        local raycastParams = RaycastParams.new()
        raycastParams.FilterDescendantsInstances = {plr.Character}
        raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

        local rayResult = workspace:Raycast(firePoint, direction * range, raycastParams)

        local bullet = Instance.new("Part")
        bullet.Size = Vector3.new(0.1, 0.1, (rayResult and (rayResult.Position - firePoint).Magnitude or range))
        bullet.CFrame = CFrame.new(firePoint, mousePos) * CFrame.new(0, 0, -bullet.Size.Z / 2)
        bullet.Anchored = true
        bullet.Transparency = 0.9
        bullet.CanCollide = false
        bullet.BrickColor = BrickColor.new("White")
        bullet.Parent = workspace

        ammo = ammo - 1
        valuesFold.Ammo.Value = ammo
    end
end)

What “trail” are you referring to?