local g=function(g,V)local S=g:FindFirstChild(V)while S==nil do g.ChildAdded:wait()S=g:FindFirstChild(V)if S then break end end return S end local V={neckC1=nil;rshoC0=nil,lshoC0=nil,rhipC0=nil,lhipC0=nil}local S=function(S,I)local J=g(I,"\084\111\114\115\111")local R=g(J,"\078\101\099\107")local s=g(J,"\082\105\103\104\116 \083\104\111\117\108\100\101\114")local j=g(J,"\076\101\102\116 \083\104\111\117\108\100\101\114")local X=g(J,"\082\105\103\104\116 \072\105\112")local l=g(J,"\076\101\102\116 \072\105\112")local m=script.Parent.Configuration local D=m["\072\101\097\100 \065\110\103\108\101"].Value local H=m["\076\101\103\115 \065\110\103\108\101"].Value local i=m["\065\114\109\115 \065\110\103\108\101"].Value local h=m["\083\105\116\116\105\110\103 \065\110\103\108\101"].Value local C=m["\083\105\116\116\105\110\103 \080\111\115\105\116\105\111\110"].Value S.C0=S.C0*CFrame.new(0,1,0)V.neckC1=R.C1 V.rshoC0=s.C0 V.lshoC0=j.C0 V.rhipC0=X.C0 V.lhipC0=l.C0 R.C1=R.C1*CFrame.fromEulerAnglesXYZ(math.rad(D),0,0)s.C0=s.C0*CFrame.fromEulerAnglesXYZ(0,0,0)j.C0=j.C0*CFrame.fromEulerAnglesXYZ(0,math.rad(i),.3)j.C0=j.C0*CFrame.new(-1,0,-0.5)X.C0=X.C0*CFrame.fromEulerAnglesXYZ(0,0,math.rad(H))l.C0=l.C0*CFrame.fromEulerAnglesXYZ(0,0,math.rad(-H))end local I=function(S,I)local J=g(I,"\084\111\114\115\111")local R=g(J,"\078\101\099\107")local s=g(J,"\082\105\103\104\116 \083\104\111\117\108\100\101\114")local j=g(J,"\076\101\102\116 \083\104\111\117\108\100\101\114")local X=g(J,"\082\105\103\104\116 \072\105\112")local l=g(J,"\076\101\102\116 \072\105\112")R.C1=V.neckC1 or CFrame.new()s.C0=V.rshoC0 or CFrame.new()j.C0=V.lshoC0 or CFrame.new()X.C0=V.rhipC0 or CFrame.new()l.C0=V.lhipC0 or CFrame.new()S:Destroy()end script.Parent.ChildAdded:connect(function(g)if g:IsA("\087\101\108\100")then local V=nil if g.Part1~=nil and(g.Part1.Parent~=nil and g.Part1.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")~=nil)then V=g.Part1.Parent else return end S(g,V)end end)script.Parent.ChildRemoved:connect(function(g)if g:IsA("\087\101\108\100")then local V=nil if g.Part1~=nil and(g.Part1.Parent~=nil and g.Part1.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")~=nil)then V=g.Part1.Parent else return end I(g,V)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)
	weld.C0 = weld.C0 * CFrame.new(0, 1, 0)

	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(0, 0, 0)
	lsho.C0 = lsho.C0 * CFrame.fromEulerAnglesXYZ(0,math.rad(arms_ang), 0.3)
	lsho.C0 = lsho.C0 * CFrame.new(-1,0, -0.5)

	--Now the legs
	rhip.C0 = rhip.C0 * CFrame.fromEulerAnglesXYZ(0, 0, math.rad(legs_ang))
	lhip.C0 = lhip.C0 * CFrame.fromEulerAnglesXYZ(0, 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)
