Aiming Using A Follow Arms Script (Please Be Good At Math Dude Or I'ma Do Something Drastic)

I need to make it so when I aim in, the aimpart of the gun and the arms are moved so that it looks like you’re aiming

I suck at math so I don’t know how to line it up correctly.

wait()

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:wait()
end
local humanoid = character:WaitForChild("Humanoid")
local torso = character:WaitForChild("Torso")
local rightShoulder = torso:WaitForChild("Right Shoulder")
local leftShoulder = torso:WaitForChild("Left Shoulder")
local camera = game.Workspace.CurrentCamera

updateSpeed = 0.1/1

local AimPartCFrame = nil

local Tool
local AimPart

game:GetService("RunService").RenderStepped:Connect(function()
	--character["Right Arm"].LocalTransparencyModifier = character["Right Arm"].Transparency
	--character["Left Arm"].LocalTransparencyModifier = character["Left Arm"].Transparency
	local camCF = camera.CoordinateFrame
	if character:FindFirstChild("Head") and character:FindFirstChild("Right Arm") and character:FindFirstChild("Left Arm") then
		local distance = (character.Head.Position - camCF.p).magnitude
		if distance <= 2 and humanoid.Health ~= 0 and character:FindFirstChild("Humanoid").Sit == false or distance <= 2 and humanoid.Health ~= 0 and character:FindFirstChild("Humanoid").Sit == true and character:FindFirstChildOfClass("Tool") then
			if character:FindFirstChild("Aiming") and character:FindFirstChildOfClass("Tool") then
				Tool = character:FindFirstChild("Aiming") and character:FindFirstChildOfClass("Tool")
				if Tool:FindFirstChild("AimPart") and Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert") then
					local AimPart = Tool:FindFirstChild("AimPart")
					local cf = camera.CFrame
					local Difference = Tool.Handle.CFrame:ToObjectSpace(cf)

					if AimPartCFrame == nil then
						AimPartCFrame = CFrame.new(Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.X - math.floor(Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.X), Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.Y - math.floor(Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.Y) , Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.Z - math.floor(Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.Z))
					end

					if not character:FindFirstChild("Ledging") and not character:FindFirstChild("Zipping") then
						rightShoulder.C0 = rightShoulder.C0:lerp((camCF * CFrame.new(1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * AimPartCFrame * CFrame.Angles(0, math.pi/2, 0), updateSpeed)
						leftShoulder.C0 = leftShoulder.C0:lerp((camCF * CFrame.new(-1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * AimPartCFrame * CFrame.Angles(0, -math.pi/2, 0), updateSpeed)
					else
						rightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, math.pi/2, 0)
						leftShoulder.C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -math.pi/2, 0)
					end
					if game.UserInputService.MouseIconEnabled == true then
						game.UserInputService.MouseIconEnabled = false
					end
					player.PlayerGui:WaitForChild("Crosshair").Enabled = true

				end
			end
		else
			if AimPartCFrame ~= nil then
				AimPartCFrame = nil
			end
			rightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, math.pi/2, 0)
			leftShoulder.C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -math.pi/2, 0)
			if game.UserInputService.MouseIconEnabled == false then
				game.UserInputService.MouseIconEnabled = true
			end
			player.PlayerGui:WaitForChild("Crosshair").Enabled = false
		end
	end
end)
1 Like

Could you try describing the issue again? It’s not clear to me what you want to achieve exactly.

Immediately I notice your camCF variable is accessing the wrong property name of the camera. Should just be CFrame.
FYI I’m on mobile so I might not be great help.

There’s nothing wrong with code cause it works. I’m just trying to figure out how I can make it so when I aim in, the aimpart is to the center of the camera. So it looks like it’s aiming, but it needs to use the right and left shoulder too.

Let me rephrase that.
What do you mean by “is at the centre of the camera”? Do you mean a certain distance in front of the camera, or using rays to place it on surfaces?

I literally just want to make it so when you’re aiming, your camera is inside and looking through the aimpart. I just want the arms moved with into that position too.

If you want the camera to be placed behind the aim part, it should be a simple CFrame offset operation. However that’s probably not what you mean because you seem to understand how CFrame offsets work already.

Yeah I tried that, but obviously the gun’s aimparts don’t have the same CFrame so when I switch to another gun the CFrame doesn’t match up at all. So that’s why I’m trying to get some type of equation that’ll line up the aimpart no matter where it’s located.

Alright, well you already seem to do this with the welds but I’ll detail it here anyway.

local newCFrame = aimPartCF * Vector3.new(0, 0, 1)

This should return a cframe with the same orientation as aimPart but offset 1 stud behind (or ahead of) it on the depth axis. Assuming the front of the aimPart points forward, that is.

This code assumes that Roblox’s “front” is actually negative Z. I need to verify it, but it seems to be one of Roblox’s quirks. If the CFrame is instead in front of the aimPart, just make the 1 negative.

It didn’t do much for me, but I also might’ve added it on wrong.

if AimPartCFrame == nil then
						AimPartCFrame = CFrame.new(Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.X, Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.Y, Tool:FindFirstChild("AimPart"):FindFirstChild("CFrame_Insert").Value.Z)
						AimPartCFrame = AimPartCFrame * Vector3.new(0, 0, 1)
					end

					if not character:FindFirstChild("Ledging") and not character:FindFirstChild("Zipping") then
						rightShoulder.C0 = rightShoulder.C0:lerp((camCF * CFrame.new(1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * AimPartCFrame * CFrame.Angles(0, math.pi/2, 0), updateSpeed)
						leftShoulder.C0 = leftShoulder.C0:lerp((camCF * CFrame.new(-1, -1, 0)):toObjectSpace(torso.CFrame):inverse() * AimPartCFrame * CFrame.Angles(0, -math.pi/2, 0), updateSpeed)
					else
						rightShoulder.C0 = CFrame.new(1, 0.5, 0) * CFrame.Angles(0, math.pi/2, 0)
						leftShoulder.C0 = CFrame.new(-1, 0.5, 0) * CFrame.Angles(0, -math.pi/2, 0)
					end

It also says I can’t multiply the CFrame by the vector3 so uuuhhhh that’s pretty bad news too.

This is what happens when I directly set the X, Y, Z values specifically.

I want to make it so all the guns follow the same formula to rotate and be positioned the same way through the aimpart (if anyone thought my annoying tag didn’t make sense, it does now).

right yeah you’ll want to create a cframe from that last vector3, my bad

Yeah sorry man it didn’t work. This stuff is super confusing maann…

I have yet to design a first-person gun viewmodel. I’m not familiar with how the effects are achieved.

I’m going to sleep now, but please demonstrate what you are currently getting, what you are looking for and then list where you think the issue is and what you have tried so far. The more context you provide, the more help we can give you.

Edit: also, please add comments to your code so we can understand why you’ve written things in certain ways!