Reducing lag on arm movement script

I have a code in my client script that moves the arms up and down to the mouse. I’ve been tweaking the values for them for quite a while but the arms move more laggy rather than smooth. I’m wondering if there is a way to improve this code.

Resetting the players arms script:

local function ResetCharMovement()
	local Neck = Character:FindFirstChild("Neck", true)
	Neck.C0 = CFrame.Angles(math.pi/2, math.pi, 0) + Vector3.new(Neck.C0.X, Neck.C0.Y, Neck.C0.Z)
	
	local Arm = Character:FindFirstChild("Right Shoulder", true)
	Arm.C0 = CFrame.Angles(0, math.pi/2, 0) + Vector3.new(Arm.C0.X, Arm.C0.Y, Arm.C0.Z)
	
	local LeftArm = Character:FindFirstChild("Left Shoulder", true)
	LeftArm.C0 = CFrame.Angles(0, -math.pi/2, 0) + Vector3.new(LeftArm.C0.X, LeftArm.C0.Y, LeftArm.C0.Z)
end

Moving the arms itself:

local function UpdateCharMovement()
	local dir = (Mouse.Hit.p - Character.Head.Position).Unit
	local angle = math.acos(dir:Dot(Vector3.new(0, 1, 0))) - math.pi/2
	angle = math.min(math.max(- math.pi/5, angle), math.pi/5)
	
	local Neck = Character:FindFirstChild("Neck", true)
	Neck.C0 = CFrame.Angles(- angle + math.pi/2, math.pi, 0) + Vector3.new(0, Neck.C0.Y, 0)
	
	local Arm = Character:FindFirstChild("Right Shoulder", true)
	Arm.C0 = CFrame.Angles(- angle, math.pi/2 + math.pi/40 + math.pi/32, math.pi/36) + Vector3.new(Arm.C0.X, Arm.C0.Y, Arm.C0.Z)
	
	local LeftArm = Character:FindFirstChild("Left Shoulder", true)
	LeftArm.C0 = CFrame.Angles(- angle, -math.pi/2 - math.pi/40, -math.pi/36) + Vector3.new(LeftArm.C0.X, LeftArm.C0.Y, LeftArm.C0.Z)
end

Thanks! :slightly_smiling_face:

Move your variables for Neck, Arm, and LeftArm outside of the function so it doesn’t constantly re-find them, it’ll help a small amount.
If the script isn’t local you should consider making it a local script.
Also if you aren’t using renderstepped and the lag you’re talking about is the arms looking choppy, try using renderstepped instead of wait().

1 Like

But it in #help-and-feedback:code-review ?

To answer the question, save the variables outside the function.

local function limbCFrame(bodyPart)
bodyPart.C0 = CFrame.Angles(- angle, math.pi/2 + math.pi/40 + math.pi/32, math.pi/36) + Vector3.new(bodyPart.C0.X, bodyPart.C0.Y, bodyPart.C0.Z)
end

Also something I find weird is that you put a boolean inside the parameters of FindFirstChild. When it finds a child, it is already set to true?..

This smoothed it out a lot. However, I still have this major bug when your a little bit zoomed it. I’m not sure whats causing it.