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
	end)

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
image

code:

local function ConvertToVec2(Vector)
		local Camera = game.Workspace.CurrentCamera
		local screenSize = Camera.ViewportSize
		Vector = (Vector2.new(Vector.X, Vector.Y) - screenSize/2) * (2/screenSize)
		return Vector3.new(Vector.X, -Vector.Y, 0)
	end
	game.ReplicatedStorage.RemoteEvents.MousePosition.OnServerEvent:Connect(function()
		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)
		end

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