Need help with broken Arm and Head moving system

So, I made a Arm and Head moving system, however, when player died, the Arm and Head moving system will broken and throw lots of error. Here my script code:

run.RenderStepped:Connect(function()

	if equipped then
		local rightX, rightY, rightZ = char.Torso["Right Shoulder"].C0:ToEulerAnglesYXZ()
		char.Torso["Right Shoulder"].C0 = (char.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((mouse.Hit.p - mouse.Origin.p).unit.y))
		local Camera = workspace.CurrentCamera
		local Player = game.Players.LocalPlayer

		local Character = Player.Character
		local Root = Character:WaitForChild("HumanoidRootPart")
		local Neck = Character:FindFirstChild("Neck", true)
		local YOffset = Neck.C0.Y

		local CFNew, CFAng = CFrame.new, CFrame.Angles
		local asin = math.asin

		game:GetService("RunService").RenderStepped:Connect(function()
			local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
			if Neck then
				if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
					Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
				elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
					Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
				end
			end
		end)
	end
end)

Just check if the Humanoids Health is above 0, also define all of your player variables at the start. Its good practice

1 Like

It would help to see the errors but… the errors of the script shouldn’t matter as long as the script is in StarterCharacterScripts. In other terms, The script that had the errors will just get deleted, and a new copy of the script will be added to the alive character when they respawn.

Also @mrkomps soloution having an if statement to check the health will work too

Forgot to mention, the script is in tool.

@StraightScared is right. It should be in a startPlayerScript. If theres multiple tools with that script it would lower the intensity of the other ones. Atleast from my experience

I tried it, but doesn’t work. Or maybe I mess up something, could you write one for me?

try something like this.

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:FindFirstChild("Neck", true)
local Humanoid = Character:WaitForChild("Humanoid")

run.RenderStepped:Connect(function()

	if equipped and Humanoid.Health > 0 then
		local rightX, rightY, rightZ = char.Torso["Right Shoulder"].C0:ToEulerAnglesYXZ()
		char.Torso["Right Shoulder"].C0 = (char.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((mouse.Hit.p - mouse.Origin.p).unit.y))

		local YOffset = Neck.C0.Y

		local CFNew, CFAng = CFrame.new, CFrame.Angles
		local asin = math.asin

		game:GetService("RunService").RenderStepped:Connect(function()
			local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
			if Neck then
				if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
					Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
				elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
					Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
				end
			end
		end)
	end
end)

Still does’t work. Also, here the error message:

  18:17:17.200  Right Shoulder is not a valid member of Part "3JUMPABuser.Torso"  -  Client - ToolScript:71
  18:17:17.201  Stack Begin  -  Studio
  18:17:17.202  Script 'Players.3JUMPABuser.Backpack.Pistol.ToolScript', Line 71  -  Studio - ToolScript:71
  18:17:17.203  Stack End  -  Studio

sorry I only just realised you had a runservice in a runservice. This would destroy your game out of lag

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Root = Character:WaitForChild("HumanoidRootPart")
local Neck = Character:FindFirstChild("Neck", true)
local Humanoid = Character:WaitForChild("Humanoid")

game:GetService("RunService").RenderStepped:Connect(function()
	
	if equipped and Humanoid.Health > 0 then

		local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector

		local rightX, rightY, rightZ = Character.Torso["Right Shoulder"].C0:ToEulerAnglesYXZ()
		Character.Torso["Right Shoulder"].C0 = (Character.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((mouse.Hit.p - mouse.Origin.p).unit.y))

		local YOffset = Neck.C0.Y

		local CFNew, CFAng = CFrame.new, CFrame.Angles
		local asin = math.asin


		if Neck then
			if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
				Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
			elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
				Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
			end
		end

	end

end)
1 Like

If RenderStepped fires at a variable frequency of 40 times per frame, nesting two would result in the inner connection being fired 1,600 times per frame.

I know, I just edited the script he sent then only realised that after

Oh yeah, I wasn’t critiquing your scripts I was just letting the thread’s poster know for future reference.

oh, my bad. Just was a bit confused last night