function stopAllAnimations()local O=currentAnim if emoteNames[O]~=nil and emoteNames[O]==false then O="\105\100\108\101"end if currentlyPlayingEmote then O="\105\100\108\101"currentlyPlayingEmote=false end currentAnim=""currentAnimInstance=nil if currentAnimKeyframeHandler~=nil then currentAnimKeyframeHandler:disconnect()end if currentAnimTrack~=nil then currentAnimTrack:Stop()currentAnimTrack:Destroy()currentAnimTrack=nil end if runAnimKeyframeHandler~=nil then runAnimKeyframeHandler:disconnect()end if runAnimTrack~=nil then runAnimTrack:Stop()runAnimTrack:Destroy()runAnimTrack=nil end return O end function getHeightScale()if Humanoid then if not Humanoid.AutomaticScalingEnabled then return 1 end local O=Humanoid.HipHeight/HumanoidHipHeight if AnimationSpeedDampeningObject==nil then AnimationSpeedDampeningObject=script:FindFirstChild("\083\099\097\108\101\068\097\109\112\101\110\105\110\103\080\101\114\099\101\110\116")end if AnimationSpeedDampeningObject~=nil then O=1+((Humanoid.HipHeight-HumanoidHipHeight)*AnimationSpeedDampeningObject.Value)/HumanoidHipHeight end return O end return 1 end local function O(O)local K=O*1.25 local a=getHeightScale()local R=K/a return R end local function K(O)local K=(O*1.25)/getHeightScale()if K~=currentAnimSpeed then if K<.33 then currentAnimTrack:AdjustWeight(1)runAnimTrack:AdjustWeight(.0001)elseif K<.66 then local O=(K-.33)/.33 currentAnimTrack:AdjustWeight((1-O)+.0001)runAnimTrack:AdjustWeight(O+.0001)else currentAnimTrack:AdjustWeight(.0001)runAnimTrack:AdjustWeight(1)end currentAnimSpeed=K runAnimTrack:AdjustSpeed(K)currentAnimTrack:AdjustSpeed(K)end end function setAnimationSpeed(O)if currentAnim=="\119\097\108\107"then K(O)else if O~=currentAnimSpeed then currentAnimSpeed=O currentAnimTrack:AdjustSpeed(currentAnimSpeed)end end end function keyFrameReachedFunc(O)if O=="\069\110\100"then if currentAnim=="\119\097\108\107"then if userNoUpdateOnLoop==true then if runAnimTrack.Looped~=true then runAnimTrack.TimePosition=.0 end if currentAnimTrack.Looped~=true then currentAnimTrack.TimePosition=.0 end else runAnimTrack.TimePosition=.0 currentAnimTrack.TimePosition=.0 end else local O=currentAnim if emoteNames[O]~=nil and emoteNames[O]==false then O="\105\100\108\101"end if currentlyPlayingEmote then if currentAnimTrack.Looped then return end O="\105\100\108\101"currentlyPlayingEmote=false end local K=currentAnimSpeed playAnimation(O,.15,Humanoid)setAnimationSpeed(K)end end end function rollAnimation(O)local K=math.random(1,animTable[O].totalWeight)local a=K local R=1 while K>animTable[O][R].weight do K=K-animTable[O][R].weight R=R+1 end return R end local function a(O,K,a,R)if O~=currentAnimInstance then if currentAnimTrack~=nil then currentAnimTrack:Stop(a)currentAnimTrack:Destroy()end if runAnimTrack~=nil then runAnimTrack:Stop(a)runAnimTrack:Destroy()if userNoUpdateOnLoop==true then runAnimTrack=nil end end currentAnimSpeed=1.0 currentAnimTrack=R:LoadAnimation(O)currentAnimTrack.Priority=Enum.AnimationPriority.Core currentAnimTrack:Play(a)currentAnim=K currentAnimInstance=O if currentAnimKeyframeHandler~=nil then currentAnimKeyframeHandler:disconnect()end currentAnimKeyframeHandler=currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)if K=="\119\097\108\107"then local O="\114\117\110"local K=rollAnimation(O)runAnimTrack=R:LoadAnimation(animTable[O][K].anim)runAnimTrack.Priority=Enum.AnimationPriority.Core runAnimTrack:Play(a)if runAnimKeyframeHandler~=nil then runAnimKeyframeHandler:disconnect()end runAnimKeyframeHandler=runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)end end end function playAnimation(O,K,R)local b=rollAnimation(O)local v=animTable[O][b].anim a(v,O,K,R)currentlyPlayingEmote=false end function playEmote(O,K,R)a(O,O.Name,K,R)currentlyPlayingEmote=true end


function stopAllAnimations()
	local oldAnim = currentAnim

	-- return to idle if finishing an emote
	if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
		oldAnim = "idle"
	end
	
	if currentlyPlayingEmote then
		oldAnim = "idle"
		currentlyPlayingEmote = false
	end

	currentAnim = ""
	currentAnimInstance = nil
	if (currentAnimKeyframeHandler ~= nil) then
		currentAnimKeyframeHandler:disconnect()
	end

	if (currentAnimTrack ~= nil) then
		currentAnimTrack:Stop()
		currentAnimTrack:Destroy()
		currentAnimTrack = nil
	end

	-- clean up walk if there is one
	if (runAnimKeyframeHandler ~= nil) then
		runAnimKeyframeHandler:disconnect()
	end
	
	if (runAnimTrack ~= nil) then
		runAnimTrack:Stop()
		runAnimTrack:Destroy()
		runAnimTrack = nil
	end
	
	return oldAnim
end

function getHeightScale()
	if Humanoid then
		if not Humanoid.AutomaticScalingEnabled then
			return 1
		end
		
		local scale = Humanoid.HipHeight / HumanoidHipHeight
		if AnimationSpeedDampeningObject == nil then
			AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
		end
		if AnimationSpeedDampeningObject ~= nil then
			scale = 1 + (Humanoid.HipHeight - HumanoidHipHeight) * AnimationSpeedDampeningObject.Value / HumanoidHipHeight
		end
		return scale
	end	
	return 1
end

local function rootMotionCompensation(speed)
	local speedScaled = speed * 1.25
	local heightScale = getHeightScale()
	local runSpeed = speedScaled / heightScale
	return runSpeed
end

local function setRunSpeed(speed)
	local ActualSpeed = speed * 1.25 / getHeightScale()
	if ActualSpeed ~= currentAnimSpeed then
		if ActualSpeed < 0.33 then
			currentAnimTrack:AdjustWeight(1)
			runAnimTrack:AdjustWeight(0.0001)
		elseif ActualSpeed < 0.66 then
			local Weight = (ActualSpeed - 0.33) / 0.33
			currentAnimTrack:AdjustWeight(1 - Weight + 0.0001)
			runAnimTrack:AdjustWeight(Weight + 0.0001)
		else
			currentAnimTrack:AdjustWeight(0.0001)
			runAnimTrack:AdjustWeight(1)
		end
		currentAnimSpeed = ActualSpeed
		runAnimTrack:AdjustSpeed(ActualSpeed)
		currentAnimTrack:AdjustSpeed(ActualSpeed)
	end
end

function setAnimationSpeed(speed)
	if currentAnim == "walk" then
		setRunSpeed(speed)
	else
		if speed ~= currentAnimSpeed then
			currentAnimSpeed = speed
			currentAnimTrack:AdjustSpeed(currentAnimSpeed)
		end
	end
end

function keyFrameReachedFunc(frameName)
	if (frameName == "End") then
		if currentAnim == "walk" then
			if userNoUpdateOnLoop == true then
				if runAnimTrack.Looped ~= true then
					runAnimTrack.TimePosition = 0.0
				end
				if currentAnimTrack.Looped ~= true then
					currentAnimTrack.TimePosition = 0.0
				end
			else
				runAnimTrack.TimePosition = 0.0
				currentAnimTrack.TimePosition = 0.0
			end
		else
			local repeatAnim = currentAnim
			-- return to idle if finishing an emote
			if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
				repeatAnim = "idle"
			end
			
			if currentlyPlayingEmote then
				if currentAnimTrack.Looped then
					-- Allow the emote to loop
					return
				end
				
				repeatAnim = "idle"
				currentlyPlayingEmote = false
			end
			
			local animSpeed = currentAnimSpeed
			playAnimation(repeatAnim, 0.15, Humanoid)
			setAnimationSpeed(animSpeed)
		end
	end
end

function rollAnimation(animName)
	local roll = math.random(1, animTable[animName].totalWeight) 
	local origRoll = roll
	local idx = 1
	while (roll > animTable[animName][idx].weight) do
		roll = roll - animTable[animName][idx].weight
		idx = idx + 1
	end
	return idx
end

local function switchToAnim(anim, animName, transitionTime, humanoid)
	-- switch animation		
	if (anim ~= currentAnimInstance) then
		
		if (currentAnimTrack ~= nil) then
			currentAnimTrack:Stop(transitionTime)
			currentAnimTrack:Destroy()
		end

		if (runAnimTrack ~= nil) then
			runAnimTrack:Stop(transitionTime)
			runAnimTrack:Destroy()
			if userNoUpdateOnLoop == true then
				runAnimTrack = nil
			end
		end

		currentAnimSpeed = 1.0
	
		-- load it to the humanoid; get AnimationTrack
		currentAnimTrack = humanoid:LoadAnimation(anim)
		currentAnimTrack.Priority = Enum.AnimationPriority.Core
		 
		-- play the animation
		currentAnimTrack:Play(transitionTime)
		currentAnim = animName
		currentAnimInstance = anim

		-- set up keyframe name triggers
		if (currentAnimKeyframeHandler ~= nil) then
			currentAnimKeyframeHandler:disconnect()
		end
		currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
		
		-- check to see if we need to blend a walk/run animation
		if animName == "walk" then
			local runAnimName = "run"
			local runIdx = rollAnimation(runAnimName)

			runAnimTrack = humanoid:LoadAnimation(animTable[runAnimName][runIdx].anim)
			runAnimTrack.Priority = Enum.AnimationPriority.Core
			runAnimTrack:Play(transitionTime)	
			
			if (runAnimKeyframeHandler ~= nil) then
				runAnimKeyframeHandler:disconnect()
			end
			runAnimKeyframeHandler = runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)	
		end
	end
end

function playAnimation(animName, transitionTime, humanoid) 	
	local idx = rollAnimation(animName)
	local anim = animTable[animName][idx].anim

	switchToAnim(anim, animName, transitionTime, humanoid)
	currentlyPlayingEmote = false
end

function playEmote(emoteAnim, transitionTime, humanoid)
	switchToAnim(emoteAnim, emoteAnim.Name, transitionTime, humanoid)
	currentlyPlayingEmote = true
end
