local n={wave=false,point=false;dance=true,dance2=true,dance3=true,laugh=false,cheer=false}math.randomseed(tick())function findExistingAnimationInSet(n,F)if n==nil or F==nil then return 0 end for C=1,n.count,1 do if n[C].anim.AnimationId==F.AnimationId then return C end end return 0 end function configureAnimationSet(n,F)if animTable[n]~=nil then for n,F in pairs(animTable[n].connections)do F:disconnect()end end animTable[n]={}animTable[n].count=0 animTable[n].totalWeight=0 animTable[n].connections={}local C=true local a=script:FindFirstChild(n)if C and a~=nil then table.insert(animTable[n].connections,a.ChildAdded:connect(function(C)configureAnimationSet(n,F)end))table.insert(animTable[n].connections,a.ChildRemoved:connect(function(C)configureAnimationSet(n,F)end))local C=0 for a,E in pairs(a:GetChildren())do if E:IsA("\065\110\105\109\097\116\105\111\110")then local a=1 local l=E:FindFirstChild("\087\101\105\103\104\116")if l~=nil then a=l.Value end animTable[n].count=animTable[n].count+1 C=animTable[n].count animTable[n][C]={}animTable[n][C].anim=E animTable[n][C].weight=a animTable[n].totalWeight=animTable[n].totalWeight+animTable[n][C].weight table.insert(animTable[n].connections,E.Changed:connect(function(C)configureAnimationSet(n,F)end))table.insert(animTable[n].connections,E.ChildAdded:connect(function(C)configureAnimationSet(n,F)end))table.insert(animTable[n].connections,E.ChildRemoved:connect(function(C)configureAnimationSet(n,F)end))end end end if animTable[n].count<=0 then for F,C in pairs(F)do animTable[n][F]={}animTable[n][F].anim=Instance.new("\065\110\105\109\097\116\105\111\110")animTable[n][F].anim.Name=n animTable[n][F].anim.AnimationId=C.id animTable[n][F].weight=C.weight animTable[n].count=animTable[n].count+1 animTable[n].totalWeight=animTable[n].totalWeight+C.weight end end for n,F in pairs(animTable)do for n=1,F.count,1 do if PreloadedAnims[F[n].anim.AnimationId]==nil then Humanoid:LoadAnimation(F[n].anim)PreloadedAnims[F[n].anim.AnimationId]=true end end end end

local emoteNames = { wave = false, point = false, dance = true, dance2 = true, dance3 = true, laugh = false, cheer = false}

math.randomseed(tick())

function findExistingAnimationInSet(set, anim)
	if set == nil or anim == nil then
		return 0
	end
	
	for idx = 1, set.count, 1 do 
		if set[idx].anim.AnimationId == anim.AnimationId then
			return idx
		end
	end
	
	return 0
end

function configureAnimationSet(name, fileList)
	if (animTable[name] ~= nil) then
		for _, connection in pairs(animTable[name].connections) do
			connection:disconnect()
		end
	end
	animTable[name] = {}
	animTable[name].count = 0
	animTable[name].totalWeight = 0	
	animTable[name].connections = {}

	local allowCustomAnimations = true

	--local success, msg = pcall(function() allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations end)
	--if not success then
	--	allowCustomAnimations = true
	--end

	-- check for config values
	local config = script:FindFirstChild(name)
	if (allowCustomAnimations and config ~= nil) then
		table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
		table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
		
		local idx = 0
		for _, childPart in pairs(config:GetChildren()) do
			if (childPart:IsA("Animation")) then
				local newWeight = 1
				local weightObject = childPart:FindFirstChild("Weight")
				if (weightObject ~= nil) then
					newWeight = weightObject.Value
				end
				animTable[name].count = animTable[name].count + 1
				idx = animTable[name].count
				animTable[name][idx] = {}
				animTable[name][idx].anim = childPart
				animTable[name][idx].weight = newWeight
				animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
				table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
				table.insert(animTable[name].connections, childPart.ChildAdded:connect(function(property) configureAnimationSet(name, fileList) end))
				table.insert(animTable[name].connections, childPart.ChildRemoved:connect(function(property) configureAnimationSet(name, fileList) end))
			end
		end
	end
	
	-- fallback to defaults
	if (animTable[name].count <= 0) then
		for idx, anim in pairs(fileList) do
			animTable[name][idx] = {}
			animTable[name][idx].anim = Instance.new("Animation")
			animTable[name][idx].anim.Name = name
			animTable[name][idx].anim.AnimationId = anim.id
			animTable[name][idx].weight = anim.weight
			animTable[name].count = animTable[name].count + 1
			animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
		end
	end
	
	-- preload anims
	for i, animType in pairs(animTable) do
		for idx = 1, animType.count, 1 do
			if PreloadedAnims[animType[idx].anim.AnimationId] == nil then
				Humanoid:LoadAnimation(animType[idx].anim)
				PreloadedAnims[animType[idx].anim.AnimationId] = true
			end				
		end
	end
end
