Gun Aiming System

I’m currently working on an aiming script for my FPS gun that im making, Right now I Already have a script that makes the camera move to the impart but the problem is that I can’t move my camera up or down, left ,right.

this is the code:

local player = game:GetService(“Players”).LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local RunService = game:GetService(“RunService”)
local tool = script.Parent
local aimpart = tool.AimPart

tool.Equipped:Connect(function()

mouse.Button2Down:Connect(function()

	camera.CameraSubject = aimpart

	RunService.RenderStepped:Connect(function()
		camera.CFrame = aimpart.CFrame
	end)
end)
mouse.Button2Up:Connect(function()
	camera.CameraType = Enum.CameraType.Custom
end)

end)

tool.Unequipped:Connect(function()
camera.CameraType = Enum.CameraType.Custom
end)

does anyone know how to solve it and how I can get it the camera then moving again? also does anyone know how to get back to your normal camera position so ur not aiming anymore?

I would suggest, moving the Gun Model instead of the camera.

the thing is that my camera is on the right position on the gun but I can’t move the camera anymore. so I can aim up or down or whatever.I just stay stuck there. only my camera is stuck btw, I can still move

here is the game so u can see what happens. to aim you have to hold right mouse button.
https://www.roblox.com/games/6161944619/Gun

1 Like

did you make it public? I can’t join it because the permission levels aren’t allowing me to join.

normally it is already public.

RunService.RenderStepped:Connect(function()
camera.CFrame = aimpart.CFrame
end)

Do you forgot to Disconnect this? ^
Like

local connection

mouse.Button2Down:Connect(function()
    connection = RunService.RenderStepped:Connect(function()
	    --stuff
    end
end)

mouse.Button2Up:Connect(function()
    connection:Disconnect()
end)
1 Like

thanks, this is already a solution for half the problem. as you see in the game, for some reason I can’t move my mouse when I aim. do you know how I can fix that

this is what I have now and the start and stop aiming works as you can see in the test game.

local player = game:GetService(“Players”).LocalPlayer
local char = player.Character
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()

local RunService = game:GetService(“RunService”)

local tool = script.Parent

local aimpart = tool.AimPart

local connection

local equiped = true

tool.Equipped:Connect(function()
equiped = true
mouse.Button2Down:Connect(function()
if equiped == true then
camera.CameraSubject = aimpart

		connection = RunService.RenderStepped:Connect(function()
			camera.CFrame = aimpart.CFrame
		end)
	end
end)

end)

mouse.Button2Up:Connect(function()
connection:Disconnect()
camera.CameraSubject = char:FindFirstChild(“Humanoid”)
end)

tool.Unequipped:Connect(function()
equiped = false
connection:Disconnect()
camera.CameraSubject = char:FindFirstChild(“Humanoid”)
end)

I still don’t know why I can’t move the gun up or down when aiming, does anyone know why?

This tutorial covers the aiming part, they simply tween the AimPart of the gun to the Camera, I think what you gotta do is just manipulate the CFrames of your Arms(Motor6ds)

Making an FPS Framework: Beginner’s guide [PART 2] - Resources / Community Tutorials - DevForum | Roblox

the problem with that is that im using a tool and I think they are using viewport or at least something that isn’t a tool.

Manipulate the Motor6ds C0(CFrame) of the arms(if you’re R6, it’s located in the Torso)

I’v looked stuff up about manipulation of motor6D but I still don’t understand how I could change the frame of the arm that way and I wouldn’t know how to put the arms back either.

Make a local variable for the origin C0 of the arms then

local originArmC0 = Arm.Shoulder.C0

then when equipped(Put this in the renderstepped connection)

Arm.Shoulder.C0 = originArmC0 * CFrame.Angles()

and when unequipping, just set the Shoulder’s C0 to the origin

Arm.Shoulder.C0 = originArmC0

this really helped out a lot already but there is something wrong I think.

code:
local player = game:GetService(“Players”).LocalPlayer
local char = player.Character
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()

local RunService = game:GetService(“RunService”)

local tool = script.Parent

local Arm = char:WaitForChild(“RightUpperArm”)
local Shoulder = Arm:WaitForChild(“RightShoulder”)

local ShoulderOriginC0 = Shoulder.C1

local connection

local equiped = true

tool.Equipped:Connect(function()
equiped = true
mouse.Button2Down:Connect(function()
if equiped == true then
connection = RunService.RenderStepped:Connect(function()
char:WaitForChild(“RightUpperArm”).RightShoulder.C0 = ShoulderOriginC0 * CFrame.Angles(0,0,1)
end)
end
end)
end)

mouse.Button2Up:Connect(function()
connection:Disconnect()
char:WaitForChild(“RightUpperArm”).RightShoulder.C0 = ShoulderOriginC0
end)

tool.Unequipped:Connect(function()
equiped = false
connection:Disconnect()
char:WaitForChild(“RightUpperArm”).RightShoulder.C0 = ShoulderOriginC0
end)

I ran into 2 problems:
the first problem is that it for some reason only rotates the tool and the other one is that I already have another script that uses the shoulder his C0 so the tool follows the mouse.

1 Like

i’v uploaded what I have now to the testing game that I send I while ago

What’s wrong is that

connection = RunService.RenderStepped:Connect(function()
    char:WaitForChild(“RightUpperArm”).RightShoulder.C0 = ShoulderOriginC0 * CFrame.Angles(0,0,1)
end)

You don’t have the camera Direction

-- You can use this, INSIDE THE CONNECTION
local camDir = char.HumanoidRootPart.CFrame:ToObjectSpace(camera.CFrame).LookVector
local rot = CFrame.Angles(-math.asin(camDir.Y), math.asin(camDir.X), 0)

CF = CF * rot

it still rotates the gun instead of moving it to the face