local Q={wave=false,point=false;dance=true;dance2=true;dance3=true;laugh=false;cheer=false}math.randomseed(tick())function findExistingAnimationInSet(Q,x)if Q==nil or x==nil then return 0 end for H=1,Q.count,1 do if Q[H].anim.AnimationId==x.AnimationId then return H end end return 0 end function configureAnimationSet(Q,x)if animTable[Q]~=nil then for Q,x in pairs(animTable[Q].Connections)do x:Disconnect()end end animTable[Q]={}animTable[Q].count=0 animTable[Q].totalWeight=0 animTable[Q].Connections={}local H=true local q,E=pcall(function()H=(game:GetService("\083\116\097\114\116\101\114\080\108\097\121\101\114")).AllowCustomAnimations end)if not q then H=true end local K=script:FindFirstChild(Q)if H and K~=nil then table.insert(animTable[Q].Connections,K.ChildAdded:Connect(function(H)configureAnimationSet(Q,x)end))table.insert(animTable[Q].Connections,K.ChildRemoved:Connect(function(H)configureAnimationSet(Q,x)end))local H=0 for q,E in pairs(K:GetChildren())do if E:IsA("\065\110\105\109\097\116\105\111\110")then local q=1 local K=E:FindFirstChild("\087\101\105\103\104\116")if K~=nil then q=K.Value end animTable[Q].count=animTable[Q].count+1 H=animTable[Q].count animTable[Q][H]={}animTable[Q][H].anim=E animTable[Q][H].weight=q animTable[Q].totalWeight=animTable[Q].totalWeight+animTable[Q][H].weight table.insert(animTable[Q].Connections,E.Changed:Connect(function(H)configureAnimationSet(Q,x)end))table.insert(animTable[Q].Connections,E.ChildAdded:Connect(function(H)configureAnimationSet(Q,x)end))table.insert(animTable[Q].Connections,E.ChildRemoved:Connect(function(H)configureAnimationSet(Q,x)end))end end end if animTable[Q].count<=0 then for x,H in pairs(x)do animTable[Q][x]={}animTable[Q][x].anim=Instance.new("\065\110\105\109\097\116\105\111\110")animTable[Q][x].anim.Name=Q animTable[Q][x].anim.AnimationId=H.id animTable[Q][x].weight=H.weight animTable[Q].count=animTable[Q].count+1 animTable[Q].totalWeight=animTable[Q].totalWeight+H.weight end end for Q,x in pairs(animTable)do for Q=1,x.count,1 do if PreloadedAnims[x[Q].anim.AnimationId]==nil then Humanoid:LoadAnimation(x[Q].anim)PreloadedAnims[x[Q].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
