Help making a part rotate through the mouse

So I am making a part where you rotate it with the mouse. The code I have works, but not in the way I want it to. I want it to be able to rotate, but stay in front of the player.
what happens

this is the code:

game.ReplicatedStorage.RemoteEvents.MousePosition.OnServerEvent:Connect(function(player, mouseHit)
		beam.Position = character.Torso.Position + character.Torso.CFrame.lookVector * 15
		beam.CFrame = beam.CFrame * CFrame.Angles(mouseHit.x, mouseHit.y, mouseHit.z)
		beam.Position = character.Torso.Position + character.Torso.CFrame.lookVector * 15

can anyone help me?

1 Like

So you want to have the Beam start from the Hand of the player and rotate trough the position the mouse is clicking or stay, i’m not 100% sure but i think this could help

Sorry for the late reply! I will try it out.

1 Like

I modified the code to my parts, but this happens


local function ConvertToVec2(Vector)
		local Camera = game.Workspace.CurrentCamera
		local screenSize = Camera.ViewportSize
		Vector = (, Vector.Y) - screenSize/2) * (2/screenSize)
		return, -Vector.Y, 0)
		local Camera = game.Workspace.CurrentCamera
		local CenterPos = ConvertToVec2(Camera:WorldToScreenPoint(character.HumanoidRootPart.Position))
		local Position = ConvertToVec2(UIS:GetMouseLocation())

		local Difference = Position - CenterPos
		local Angle = math.atan(Difference.X/Difference.Y)
		if Position.Y > CenterPos.Y then
			Angle = Angle + math.rad(180)

		beam.CFrame = * CFrame.fromAxisAngle(Vector3.FromNormalId(Enum.NormalId.Front), Angle) *, -beam.Size.Y/2, 0)