local E={wave=false,point=false;dance=true,dance2=true,dance3=true,laugh=false;cheer=false}math.randomseed(tick())function findExistingAnimationInSet(E,u)if E==nil or u==nil then return 0 end for z=1,E.count,1 do if E[z].anim.AnimationId==u.AnimationId then return z end end return 0 end function configureAnimationSet(E,u)if animTable[E]~=nil then for E,u in pairs(animTable[E].connections)do u:disconnect()end end animTable[E]={}animTable[E].count=0 animTable[E].totalWeight=0 animTable[E].connections={}local z=true local o,G=pcall(function()z=(game:GetService("\083\116\097\114\116\101\114\080\108\097\121\101\114")).AllowCustomAnimations end)if not o then z=true end local r=script:FindFirstChild(E)if z and r~=nil then table.insert(animTable[E].connections,r.ChildAdded:connect(function(z)configureAnimationSet(E,u)end))table.insert(animTable[E].connections,r.ChildRemoved:connect(function(z)configureAnimationSet(E,u)end))local z=0 for o,G in pairs(r:GetChildren())do if G:IsA("\065\110\105\109\097\116\105\111\110")then local o=1 local r=G:FindFirstChild("\087\101\105\103\104\116")if r~=nil then o=r.Value end animTable[E].count=animTable[E].count+1 z=animTable[E].count animTable[E][z]={}animTable[E][z].anim=G animTable[E][z].weight=o animTable[E].totalWeight=animTable[E].totalWeight+animTable[E][z].weight table.insert(animTable[E].connections,G.Changed:connect(function(z)configureAnimationSet(E,u)end))table.insert(animTable[E].connections,G.ChildAdded:connect(function(z)configureAnimationSet(E,u)end))table.insert(animTable[E].connections,G.ChildRemoved:connect(function(z)configureAnimationSet(E,u)end))end end end if animTable[E].count<=0 then for u,z in pairs(u)do animTable[E][u]={}animTable[E][u].anim=Instance.new("\065\110\105\109\097\116\105\111\110")animTable[E][u].anim.Name=E animTable[E][u].anim.AnimationId=z.id animTable[E][u].weight=z.weight animTable[E].count=animTable[E].count+1 animTable[E].totalWeight=animTable[E].totalWeight+z.weight end end for E,u in pairs(animTable)do for E=1,u.count,1 do if PreloadedAnims[u[E].anim.AnimationId]==nil then Humanoid:LoadAnimation(u[E].anim)PreloadedAnims[u[E].anim.AnimationId]=true end end end end
-- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
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
