How to make a particle appear at the end of a raycast?

I recently started learning ray-casting
I’m making a gun and a gunshot particle will appear at where you shot.
But how do I find the end of the raycast?

local ray = Ray.new(script.Parent.Position, Vector3.new(0,30,0))
local m = game.Players.LocalPlayer:GetMouse()
while wait(1) do
	local hit,position = workspace:findPartOnRay(ray, (m.Hit.p))
	if hit then
		print(hit.Name)
	end
end

Create a part with Instance.new() right where the ray ends, put a particle emitter in that part, emit it, then delete the part. If you need explaining on how to do that lmk.

That isnt the question. I need to find out how to get where the ray ends.

“position” is where the ray ends. You should also use workspace:Raycast()

You already have the position. Also as someone already said you should avoid using Ray.new() and instead use workspace:Raycast()

Okay, sorry. I was just following a tutorial and didn’t know what position was for!

If you need help creating the emitter lmk

Well. Theres a bit of an error
it says unable to cast value to object

local m = game.Players.LocalPlayer:GetMouse()
local ray = workspace:RayCast(script.Parent.HumanoidRootPart.Position,m.Hit.Position)
while wait(1) do
	local hit,position = workspace:findPartOnRay(ray, m.Hit.Position)
	if hit then
		print(hit.Name)
		local part = Instance.new("Part")
		part.Position = position
		part.Parent = workspace
		part.Anchored=true
	end
end