local D={wave=false;point=false,dance=true;dance2=true,dance3=true,laugh=false;cheer=false}local n=false local X={}local b,J=pcall(function()n=(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\080\114\101\108\111\097\100\065\110\105\109\097\116\105\111\110\115")end)if not b then n=false end math.randomseed(tick())function findExistingAnimationInSet(D,n)if D==nil or n==nil then return 0 end for X=1,D.count,1 do if D[X].anim.AnimationId==n.AnimationId then return X end end return 0 end function configureAnimationSet(D,b)if animTable[D]~=nil then for D,n in pairs(animTable[D].connections)do n:disconnect()end end animTable[D]={}animTable[D].count=0 animTable[D].totalWeight=0 animTable[D].connections={}local J=true local r=false local G,e=pcall(function()r=(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\065\108\108\111\119\068\105\115\097\098\108\101\067\117\115\116\111\109\065\110\105\109\115\050")end)if r then local D,n=pcall(function()J=(game:GetService("\083\116\097\114\116\101\114\080\108\097\121\101\114")).AllowCustomAnimations end)if not D then J=true end end local y=script:FindFirstChild(D)if J and y~=nil then table.insert(animTable[D].connections,y.ChildAdded:connect(function(n)configureAnimationSet(D,b)end))table.insert(animTable[D].connections,y.ChildRemoved:connect(function(n)configureAnimationSet(D,b)end))local n=0 for X,J in pairs(y:GetChildren())do if J:IsA("\065\110\105\109\097\116\105\111\110")then local X=1 local r=J:FindFirstChild("\087\101\105\103\104\116")if r~=nil then X=r.Value end animTable[D].count=animTable[D].count+1 n=animTable[D].count animTable[D][n]={}animTable[D][n].anim=J animTable[D][n].weight=X animTable[D].totalWeight=animTable[D].totalWeight+animTable[D][n].weight table.insert(animTable[D].connections,J.Changed:connect(function(n)configureAnimationSet(D,b)end))table.insert(animTable[D].connections,J.ChildAdded:connect(function(n)configureAnimationSet(D,b)end))table.insert(animTable[D].connections,J.ChildRemoved:connect(function(n)configureAnimationSet(D,b)end))end end end if animTable[D].count<=0 then for n,X in pairs(b)do animTable[D][n]={}animTable[D][n].anim=Instance.new("\065\110\105\109\097\116\105\111\110")animTable[D][n].anim.Name=D animTable[D][n].anim.AnimationId=X.id animTable[D][n].weight=X.weight animTable[D].count=animTable[D].count+1 animTable[D].totalWeight=animTable[D].totalWeight+X.weight end end if n then for D,n in pairs(animTable)do for D=1,n.count,1 do if X[n[D].anim.AnimationId]==nil then Humanoid:LoadAnimation(n[D].anim)X[n[D].anim.AnimationId]=true end 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 }

local PreloadAnimsUserFlag = false
local PreloadedAnims = {}
local successPreloadAnim, msgPreloadAnim = pcall(function()
	PreloadAnimsUserFlag = UserSettings():IsUserFeatureEnabled("UserPreloadAnimations")
end)
if not successPreloadAnim then
	PreloadAnimsUserFlag = false
end

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 AllowDisableCustomAnimsUserFlag = false

	local success, msg = pcall(function()
		AllowDisableCustomAnimsUserFlag = UserSettings():IsUserFeatureEnabled("UserAllowDisableCustomAnims2")
	end)

	if AllowDisableCustomAnimsUserFlag then
		local success, msg = pcall(function()
			allowCustomAnimations = game:GetService("StarterPlayer").AllowCustomAnimations
		end)
		if not success then
			allowCustomAnimations = true
		end
	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
	if PreloadAnimsUserFlag then
		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
end
