Make beam from part direction

I would like to know how I can have a part, and make a function, eg.

function beamFromPosition(cf, distance)
   
end

I would like to know how to make this function create a beam from cf, with the direction of cf, going forward distance. Eg. if you did

local cf = CFrame.new(0, 0, 0) * CFrame.Angles(math.rad(90), 0, 0)
beamFromPosition(cf, 100)

Then it would create a beam going 100 studs forward from the direction and position of cf. I have tried to make this, but I got stuck a lot, any help would be much appreciated!

Try this:

function beamFromPosition(cf, distance)
   --create the beam part
      Beam.CFrame = cf:ToWorldSpace(CFrame.new(0,0,-distance))
Beam.Size = Vector3.new(width, height,distance/2)
end

Sorry, wasn’t descriptive enough. I meant the beam effect, with attachments.

Oh, ok. That is easy, just create a new anchored part and 2 attachments in it(I will call them startBeam and endBeam) set the part’s CFrame to the desired CFrame and makes the endBeam have a position of (0,0,-distance)

You may want to fix that typo, mods might not like that.

Could you show me with code because I can’t entirely understand how this would work.