Does anyone know how to make it so that this "laser raycast" "strectches" itself in size from a part's position to a Mouse.Hit.Position?

-- Local script for raycasting
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local SoundService = game:GetService("SoundService")

local Pistol = script.Parent
local Handle = script.Parent:WaitForChild("Handle")
local BulletRayCastPart = Pistol:WaitForChild("BulletRay")
local PistolFiredEvent = script:WaitForChild("PistolFired")

local LocalPlayer = game.Players.LocalPlayer
local PlayerCamera = workspace.CurrentCamera
local Mouse = LocalPlayer:GetMouse()
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")

local Flash = Handle:WaitForChild("Flash")

local Sounds = {
	EquipSound = Handle:WaitForChild("EquipSound2"),
	UnequipSound = Handle:WaitForChild("UnequipSound"),
	ReloadSound = Handle:WaitForChild("Reload"),
	TickSound = Handle:WaitForChild("Tick"),
	FireSound = Handle:WaitForChild("FireSound")
}

local GunGui = Pistol:WaitForChild("AmmunitionGui"):Clone()
GunGui.Parent = PlayerGui

local BackgroundFrame = GunGui:WaitForChild("BackgroundFrame")
local Bullets = GunGui:WaitForChild("Bullets")
local BulletText = BackgroundFrame:WaitForChild("BulletsText")

local Shootingdebounce = false
local ReloadDebounce = false
local Equipped = false

local function UpdateBullets()
	BulletText.Text = Bullets.Value.. "/10"
end

local function Fired()
	Sounds["FireSound"]:Play()
	Bullets.Value -= 1
	Flash.Enabled = true
	task.wait(0.1)
	Flash.Enabled = false
end

local function onEquipped()
	Equipped = true
	Sounds["EquipSound"]:Play()
	GunGui.Enabled = true
end

local function onUnequipped()
	Equipped = false
	Sounds["UnequipSound"]:Play()
	GunGui.Enabled = false
end

local function onReload(input, processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.R and Equipped == true then
		if Bullets.Value < 10 and ReloadDebounce == false then
			ReloadDebounce = true
			Sounds["ReloadSound"]:Play()
			task.wait(2)
			Bullets.Value = 10
			ReloadDebounce = false
		else
			return
		end
	else
		return
	end
end

local function onActivated()
	if Shootingdebounce == true or ReloadDebounce == true then return end
	if Bullets.Value > 0 then
		Shootingdebounce = true
		task.spawn(Fired)
		
		local RayCastParameters = RaycastParams.new()
		RayCastParameters.FilterType = Enum.RaycastFilterType.Exclude
		RayCastParameters.FilterDescendantsInstances = {LocalPlayer.Character, script.Parent}
		RayCastParameters.IgnoreWater = true
		
		local Origin = BulletRayCastPart.Position
		local Direction = (Mouse.Hit.Position - BulletRayCastPart.Position).Unit * 500
		local RayResults = game.Workspace:Raycast(Origin, Direction, RayCastParameters)
		
		if not RayResults then
			PistolFiredEvent:FireServer(Mouse.Hit.Position, (Origin + Direction).Unit * 500)
		else
			PistolFiredEvent:FireServer(RayResults.Instance, RayResults.Position, RayResults.Distance)
		end
		
		task.wait(0.2)
		Shootingdebounce = false
	end
end

Pistol.Equipped:Connect(onEquipped)
Pistol.Unequipped:Connect(onUnequipped)
Pistol.Activated:Connect(onActivated)
UserInputService.InputBegan:Connect(onReload)
Bullets.Changed:Connect(UpdateBullets)
-- Sever script for creating the laser
local Debris = game:GetService("Debris")
local SoundService = game:GetService("SoundService")

local PistolFiredEvent = script.Parent
local HeadshotSound = PistolFiredEvent:WaitForChild("Headshot")

local Pistol = PistolFiredEvent.Parent.Parent
local BulletRayCastPart = Pistol:WaitForChild("BulletRay")

local function CreateLaser(Origin, targetInstance, EndPosition, Distance)
	local Laser = Instance.new("Part")
	Laser.Size = Vector3.new(0.1, 0.1, Distance)
	Laser.Material = Enum.Material.Neon
	Laser.Transparency = 0.5
	Laser.Anchored = true
	Laser.CanCollide = false
	Laser.CanTouch = false
	
	task.spawn(function()
		if targetInstance == nil then Laser.Color = Color3.fromRGB(255, 0, 0) return end
		
		if targetInstance.Name == "Head" then
			Laser.Color = Color3.fromRGB(255, 255, 0)
			local HeadshotSoundClone = HeadshotSound:Clone()
			HeadshotSoundClone.Parent = targetInstance
			HeadshotSoundClone:Play()
			Debris:AddItem(HeadshotSoundClone, 2)
		else
			Laser.Color = Color3.fromRGB(0, 255, 0)
		end
	end)

	Laser.CFrame = CFrame.lookAt(BulletRayCastPart.Position, EndPosition) * CFrame.new(Vector3.new(0,0,-Distance/2))
	Laser.Parent = game.Workspace
	Debris:AddItem(Laser, 0.1)
end

PistolFiredEvent.OnServerEvent:Connect(function(player, targetInstance, EndPosition, Distance)
	if targetInstance == nil then CreateLaser(BulletRayCastPart.Position, nil, EndPosition, Distance) return end
	task.spawn(CreateLaser, BulletRayCastPart.Position, nil, EndPosition, Distance)
	if not targetInstance.Parent:FindFirstChild("Humanoid") then return end
	
	local Humanoid = targetInstance.Parent:FindFirstChild("Humanoid")
	task.spawn(CreateLaser, BulletRayCastPart.Position, targetInstance, EndPosition, Distance)
	
	if targetInstance.Name == "Head" then
		Humanoid:TakeDamage(15)
	else
		Humanoid:TakeDamage(10)
	end
end)

The direction was already calculated. Correct:

PistolFiredEvent:FireServer(Mouse.Hit.Position, (Origin + Direction).Unit * 500)

To:

PistolFiredEvent:FireServer(Mouse.Hit.Position, Origin + Direction)

You did not modify the server-sided code to adjust to only receiving the mouse position. The origin can be assumed by the server, and the direction is calculated all the same. You can calculate the distance as the magnitude of the offset of the endpoint and origin

I tried adding an attachment to the pistol but now it’s shooting out of the ground?