local h=function(h,e)local A=h:FindFirstChild(e)while A==nil do h.ChildAdded:wait()A=h:FindFirstChild(e)if A then break end end return A end local e={neckC1=nil,rshoC0=nil,lshoC0=nil,rhipC0=nil,lhipC0=nil}local A=function(A,q)local E=h(q,"\084\111\114\115\111")local o=h(E,"\078\101\099\107")local J=h(E,"\082\105\103\104\116 \083\104\111\117\108\100\101\114")local R=h(E,"\076\101\102\116 \083\104\111\117\108\100\101\114")local V=h(E,"\082\105\103\104\116 \072\105\112")local y=h(E,"\076\101\102\116 \072\105\112")local r=script.Parent.Configuration local u=r["\072\101\097\100 \065\110\103\108\101"].Value local N=r["\076\101\103\115 \065\110\103\108\101"].Value local D=r["\065\114\109\115 \065\110\103\108\101"].Value local F=r["\083\105\116\116\105\110\103 \065\110\103\108\101"].Value local n=r["\083\105\116\116\105\110\103 \080\111\115\105\116\105\111\110"].Value A.C1=A.C1*CFrame.fromEulerAnglesXYZ(math.rad(F+90),0,0)A.C0=CFrame.new(n)e.neckC1=o.C1 e.rshoC0=J.C0 e.lshoC0=R.C0 e.rhipC0=V.C0 e.lhipC0=y.C0 o.C1=o.C1*CFrame.fromEulerAnglesXYZ(math.rad(u),0,0)J.C0=J.C0*CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),math.rad(D))R.C0=R.C0*CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),math.rad(-D))V.C0=V.C0*CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),math.rad(N))y.C0=y.C0*CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),math.rad(-N))end local q=function(A,q)local E=h(q,"\084\111\114\115\111")local o=h(E,"\078\101\099\107")local J=h(E,"\082\105\103\104\116 \083\104\111\117\108\100\101\114")local R=h(E,"\076\101\102\116 \083\104\111\117\108\100\101\114")local V=h(E,"\082\105\103\104\116 \072\105\112")local y=h(E,"\076\101\102\116 \072\105\112")o.C1=e.neckC1 or CFrame.new()J.C0=e.rshoC0 or CFrame.new()R.C0=e.lshoC0 or CFrame.new()V.C0=e.rhipC0 or CFrame.new()y.C0=e.lhipC0 or CFrame.new()A:Destroy()end script.Parent.ChildAdded:connect(function(h)if h:IsA("\087\101\108\100")then local e=nil if h.Part1~=nil and(h.Part1.Parent~=nil and h.Part1.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")~=nil)then e=h.Part1.Parent else return end A(h,e)end end)script.Parent.ChildRemoved:connect(function(h)if h:IsA("\087\101\108\100")then local e=nil if h.Part1~=nil and(h.Part1.Parent~=nil and h.Part1.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")~=nil)then e=h.Part1.Parent else return end q(h,e)end end)


local WaitFor = (function(parent, child_name)
	local found = parent:FindFirstChild(child_name)
	while found == nil do
		parent.ChildAdded:wait()
		found = parent:FindFirstChild(child_name)
		if found then break end
	end
	return found
end)

local last = { neckC1 = nil, rshoC0 = nil, lshoC0 = nil, rhipC0 = nil, lhipC0 = nil }

local ApplyModifications = (function(weld, char)
	local torso = WaitFor(char, "Torso")
	local neck = WaitFor(torso, "Neck")
	local rsho = WaitFor(torso, "Right Shoulder")
	local lsho = WaitFor(torso, "Left Shoulder")
	local rhip = WaitFor(torso, "Right Hip")
	local lhip = WaitFor(torso, "Left Hip")

	local config = script.Parent.Configuration
	local head_ang = config["Head Angle"].Value
	local legs_ang = config["Legs Angle"].Value
	local arms_ang = config["Arms Angle"].Value
	local sit_ang  = config["Sitting Angle"].Value
	local sit_pos  = config["Sitting Position"].Value

	--First adjust sitting position and angle
	--Add 90 to the angle because that's what most people will be expecting.
	weld.C1 = weld.C1 * CFrame.fromEulerAnglesXYZ(math.rad((sit_ang) + 90), 0, 0)
	
	weld.C0 = CFrame.new(sit_pos)

	last.neckC1 = neck.C1
	last.rshoC0 = rsho.C0
	last.lshoC0 = lsho.C0
	last.rhipC0 = rhip.C0
	last.lhipC0 = lhip.C0

	--Now adjust the neck angle.
	neck.C1 = neck.C1 * CFrame.fromEulerAnglesXYZ(math.rad(head_ang), 0, 0)
	

	--Now adjust the arms angle.
	rsho.C0 = rsho.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0), math.rad(0), math.rad(arms_ang))
	lsho.C0 = lsho.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0), math.rad(0), math.rad(-arms_ang))
	

	--Now the legs
	rhip.C0 = rhip.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0), math.rad(0), math.rad(legs_ang))
	lhip.C0 = lhip.C0 * CFrame.fromEulerAnglesXYZ(math.rad(0), math.rad(0), math.rad(-legs_ang))
end)

local RevertModifications = (function(weld, char)
	--Any modifications done in ApplyModifications have to be reverted here if they
	--change any welds - otherwise people will wonder why their head is pointing the wrong way.

	local torso = WaitFor(char, "Torso")
	local neck = WaitFor(torso, "Neck")
	local rsho = WaitFor(torso, "Right Shoulder")
	local lsho = WaitFor(torso, "Left Shoulder")
	local rhip = WaitFor(torso, "Right Hip")
	local lhip = WaitFor(torso, "Left Hip")


	--Now adjust the neck angle.
	neck.C1 = last.neckC1 or CFrame.new()

	rsho.C0 = last.rshoC0 or CFrame.new()
	lsho.C0 = last.lshoC0 or CFrame.new()

	rhip.C0 = last.rhipC0 or CFrame.new()
	lhip.C0 = last.lhipC0 or CFrame.new()

	weld:Destroy()
end)

script.Parent.ChildAdded:connect(function(c)
	if c:IsA("Weld") then
		local character = nil
		if c.Part1 ~= nil and c.Part1.Parent ~= nil and c.Part1.Parent:FindFirstChild("Humanoid") ~= nil then
			character = c.Part1.Parent
		else return end
		ApplyModifications(c, character)
	end
end)

script.Parent.ChildRemoved:connect(function(c)
	if c:IsA("Weld") then
		local character = nil
		if c.Part1 ~= nil and c.Part1.Parent ~= nil and c.Part1.Parent:FindFirstChild("Humanoid") ~= nil then
			character = c.Part1.Parent
		else return end
		RevertModifications(c, character)
	end
end)
