How do i make an explosion at the end of a part

sup, i’m tryna modify this gun script to summon an explosion where the beam hits, but here’s the thing:
if i just set the explosion’s position exactly to the beam’s position, the explosion is in the middle, and i don’t want that, i want it at the end of the beam which is where the bullet is hitting
here’s the script, i do not take credit for it, it’s not mine by any means but i’m tryna modify it

castray.OnServerEvent:Connect(function(fired, ray, position)
	local beam = Instance.new("Part")
	
	beam.BrickColor = BrickColor.new("Institutional white")
	beam.FormFactor = Enum.FormFactor.Custom
	beam.Material = Enum.Material.Neon
	beam.Transparency = .75
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	beam.Parent = workspace
	
	local distance = (tool:WaitForChild("FirePart").CFrame.p - position).Magnitude
	beam.Size = Vector3.new(0.1, 0.1, distance)
	beam.CFrame = CFrame.new(tool:WaitForChild("FirePart").CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
	
	game:GetService("Debris"):AddItem(beam, 0.025)
       -- this is the part that i added in
	local lol = Instance.new("Explosion",workspace)
	lol.Position = Vector3.new(beam.Position.X,beam.Position.Y,beam.Position.Z) * beam.Size.Z
	lol.BlastRadius = 5
	lol.BlastPressure = math.huge
end)

If the beam is positioned according to a player’s mouse position, then you can use the mouse’s position for the explosion

If the beam is being positioned without a known target, you’ll need to raycast to find where the end of the beam is located and position the explosion there


If position is the mouse.Hit.Position or equivalent, then doing this should fix the problem:

castray.OnServerEvent:Connect(function(fired, ray, position)
	local beam = Instance.new("Part")
	
	beam.BrickColor = BrickColor.new("Institutional white")
	beam.FormFactor = Enum.FormFactor.Custom
	beam.Material = Enum.Material.Neon
	beam.Transparency = .75
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	beam.Parent = workspace
	
	local distance = (tool:WaitForChild("FirePart").CFrame.p - position).Magnitude
	beam.Size = Vector3.new(0.1, 0.1, distance)
	beam.CFrame = CFrame.new(tool:WaitForChild("FirePart").CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
	
	game:GetService("Debris"):AddItem(beam, 0.025)
       -- this is the part that i added in
	local lol = Instance.new("Explosion")
	lol.Position = position
	lol.BlastRadius = 5
	lol.BlastPressure = math.huge
	lol.Parent = workspace
end)

that is the solution, how the hell did i not see that
thanks man