Making gun point to mouse

Hi

I was wondering how I could make a gun (the gun’s a tool) point towards the players mouse.

Thanks

1 Like

use CFrame.LookAt(position, mousepos), where position is the CFrame of the gun, and mousepos is, well, the mouse position…

Hi. Unfortunately that’s not working. This is the full code I used:

local handle = script.Parent:WaitForChild("Handle")

local run = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

tool.Equipped:Connect(function()
	
	run.Stepped:Connect(function()
		
		CFrame.lookAt(handle.Position, mouse.Hit.Position)
		
	end)
	
end)```

Thanks for the help but I have looked at both of them and there solutions is either not what I’m looking for or the rotation is buggy

1 Like

You want the gun to point where the mouse is pointing? Or literally at the mouse?

1 Like

I would like it to point to where the mouse is pointing but also have a degrees snap. The first link made the players arm snap backwards and at most angles the arm became detached from the torso

1 Like

This might be able to snap the degrees?

local cf -- CFrame result from the other tutorials

local x, y, z = cf:ToOrientation()

local degreesnap = 10

x = math.floor(x/degreesnap)*degreesnap
y = math.floor(y/degreesnap)*degreesnap
z = math.floor(z/degreesnap)*degreesnap

cf = CFrame.new(cf.Position) * CFrame.Angles(math.rad(x),math.rad(y),math.rad(z))

Could you please show me the fully code so I know where to put it

The tutorials would give you your code. The code I sent ideally will snap the rotation on the cframe.

local handle = script.Parent:WaitForChild("Handle")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

game:GetService("RunService").RenderStepped:Connect(function()
	if tool:IsDescendantOf(player.Character) then
		local cf -- CFrame result from the other tutorials

		local x, y, z = cf:ToOrientation()

		local degreesnap = 10

		x = math.floor(x/degreesnap)*degreesnap
		y = math.floor(y/degreesnap)*degreesnap
		z = math.floor(z/degreesnap)*degreesnap

		cf = CFrame.new(cf.Position) * CFrame.Angles(math.rad(x),math.rad(y),math.rad(z))
	end
end)

Hi, I think there was a bit of a misunderstanding. It’s not really a snap I wan’t more of a restrict. So that the player can’t pull there arm behind them etc. Sorry if I said it wrong.

Here is the code I got from the first tutorial:

local RunService = game:GetService("RunService")

local plr = Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()

local armOffset = char.UpperTorso.CFrame:Inverse() * char.RightUpperArm.CFrame

local armWeld = Instance.new("Weld")
armWeld.Part0 = char.UpperTorso
armWeld.Part1 = char.RightUpperArm
armWeld.Parent = char

RunService.Heartbeat:Connect(function()
	local cframe = CFrame.new(char.UpperTorso.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0)
	armWeld.C0 = armOffset * char.UpperTorso.CFrame:toObjectSpace(cframe)
end)

Could you please implement a restriction system if you can so the gun can’t move behind them

Maybe this works?

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()

local armOffset = character.UpperTorso.CFrame:Inverse() * character.RightUpperArm.CFrame

local armWeld = Instance.new("Weld")
armWeld.Part0 = character.UpperTorso
armWeld.Part1 = character.RightUpperArm
armWeld.Parent = character

game:GetService("RunService").RenderStepped:Connect(function()
	local cframe = CFrame.new(character.UpperTorso.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2)
	local armc = armOffset * character.UpperTorso.CFrame:ToObjectSpace(cframe)
	local x,y,z = armc:ToOrientation()
	y = math.clamp(y,-45,45)
	armWeld.C0 = CFrame.new(armc.Position) * CFrame.Angles(math.rad(x),math.rad(y),math.rad(z))
end)

If not, I don’t know.

No sorry. It ruins the arms position
image

You’re trying to use CFrame.lookAt to set the rotation of the handle. You can’t do that.
You can use CFrame.lookAt to create a CFrame that has the rotation you want, but then you have to use that CFrame to do something.
You can apply it to a Part using Part:SetCFrame.
You can apply it to a Tool using Tool.Handle:SetCFrame.
You can apply it to Tool.Grip and Tool.GripRight to rotate the gun and make it look like it’s aiming at the mouse.

this makes your arms follow the Y of the mouse:
RightShoulder.C0 = CFrame.new(RightShoulder.C0.Position) * CFrame.Angles(mouse.Hit.LookVector.Y,math.rad(90),0) LeftShoulder.C0 = CFrame.new(LeftShoulder.C0.Position) * CFrame.Angles(mouse.Hit.LookVector.Y,math.rad(-90),0)
this worked for me, also your characters need to be a R6