How can I make the gun point to where the mouse is?

  1. What do you want to achieve?
    For the gun to point to where the mouse is

  2. What is the issue?
    I don’t know how to do this, I did try to figure it out but it didn’t work.

  3. What solutions have you tried so far?
    There’s no documentation on this



function playAnimation(character, animation)
	print(character, animation)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local animator = humanoid:FindFirstChildOfClass("Animator")
		if animator then
			local animationTrack = animator:LoadAnimation(animation)
			animationTrack:Play()
			return animationTrack
		end
	end
end

local ReplicatedStorageService = game:GetService("ReplicatedStorage")
local PlayersService = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local LocalPlayer = PlayersService.LocalPlayer
local Mouse = LocalPlayer:GetMouse()

local Equipped = false

local reloadanim = Instance.new("Animation", script.Parent)
reloadanim.AnimationId = "rbxassetid://12520075694"
local reloadanimtrack = nil
local reloadanimpos = 0

local idleanim = Instance.new("Animation", script.Parent)
idleanim.AnimationId = "rbxassetid://12671438302"
local idleanimtrack = nil

script.Parent.Equipped:Connect(function()
	if not idleanimtrack then
		idleanimtrack = playAnimation(LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait(), idleanim)
		idleanimtrack.Looped = true
	else
		idleanimtrack:Play()
	end
	Equipped = true
	ReplicatedStorageService.ToolEvents.ConnectM6D:FireServer(script.Parent.BodyAttach)
end)

script.Parent.Unequipped:Connect(function()
	Equipped = false
	ReplicatedStorageService.ToolEvents.DisconnectM6D:FireServer()
	if reloadanimtrack then
		reloadanimpos = reloadanimtrack.TimePosition
		reloadanimtrack:Stop()
	end

	idleanimtrack:Stop()
end)

UserInputService.InputBegan:Connect(function(input, processed)
	print(input, processed)
	if processed then return end
	if input.KeyCode == Enum.KeyCode.R then
		idleanimtrack:Stop()
		if not reloadanimtrack then
			reloadanimtrack = playAnimation(LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait(), reloadanim)
		else
			idleanimtrack:Stop()
			reloadanimtrack:Play()
		end
	elseif input.KeyCode == Enum.KeyCode.F then
		idleanimtrack:Stop()
		if reloadanimtrack then
			reloadanimtrack:Stop()
		end

		script.Parent.BodyAttach.CFrame = CFrame.new(script.Parent.BodyAttach.Position, Raycast(Mouse.X, Mouse.Y))
	end
end)

function Raycast(X, Y)
	local Cam = workspace.CurrentCamera

	local Ray = Cam:ScreenPointToRay(X, Y)

	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	Params.FilterDescendantsInstances = {script.Parent.Parent}

	local Raycast = workspace:Raycast(Ray.Origin, Ray.Direction * 500, Params)

	return Raycast.Position
end

Have you tried using LookVector and combining it with Mouse.Position (Pretty sure that’s what’s called)?

What do you mean? I tried using lookAt but that doesn’t work.

I think you are doing it wrong, you have to add a LookVector and you are not defining it, I don’t know how to do that but you can probably figure that out,