function stopAllAnimations()local m=currentAnim if emoteNames[m]~=nil and emoteNames[m]==false then m="\105\100\108\101"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 m end function getHeightScale()if Humanoid then if FFlagUserAdjustHumanoidRootPartToHipPosition then if not Humanoid.AutomaticScalingEnabled then return 1 end end local m=Humanoid.HipHeight/HumanoidHipHeight if userAnimationSpeedDampening then 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 m=1+((Humanoid.HipHeight-HumanoidHipHeight)*AnimationSpeedDampeningObject.Value)/HumanoidHipHeight end end return m end return 1 end local m=.0001 function setRunSpeed(t)local l=t*4.5 local e=getHeightScale()local N=l/e if N~=currentAnimSpeed then if N<.33 then currentAnimTrack:AdjustWeight(1.0)runAnimTrack:AdjustWeight(m)elseif N<.66 then local t=(N-.33)/.33 currentAnimTrack:AdjustWeight((1.0-t)+m)runAnimTrack:AdjustWeight(t+m)else currentAnimTrack:AdjustWeight(m)runAnimTrack:AdjustWeight(1.0)end currentAnimSpeed=N runAnimTrack:AdjustSpeed(N)currentAnimTrack:AdjustSpeed(N)end end function setAnimationSpeed(m)if currentAnim=="\119\097\108\107"then setRunSpeed(m)else if m~=currentAnimSpeed then currentAnimSpeed=m currentAnimTrack:AdjustSpeed(currentAnimSpeed)end end end function keyFrameReachedFunc(m)if m=="\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 m=currentAnim if emoteNames[m]~=nil and emoteNames[m]==false then m="\105\100\108\101"end local t=currentAnimSpeed playAnimation(m,.15,Humanoid)setAnimationSpeed(t)end end end function rollAnimation(m)local t=math.random(1,animTable[m].totalWeight)local l=t local e=1 while t>animTable[m][e].weight do t=t-animTable[m][e].weight e=e+1 end return e end function playAnimation(m,t,l)local e=rollAnimation(m)local N=animTable[m][e].anim if N~=currentAnimInstance then if currentAnimTrack~=nil then currentAnimTrack:Stop(t)currentAnimTrack:Destroy()end if runAnimTrack~=nil then runAnimTrack:Stop(t)runAnimTrack:Destroy()if userNoUpdateOnLoop==true then runAnimTrack=nil end end currentAnimSpeed=1.0 currentAnimTrack=l:LoadAnimation(N)currentAnimTrack.Priority=Enum.AnimationPriority.Core currentAnimTrack:Play(t)currentAnim=m currentAnimInstance=N if currentAnimKeyframeHandler~=nil then currentAnimKeyframeHandler:disconnect()end currentAnimKeyframeHandler=currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)if m=="\119\097\108\107"then local m="\114\117\110"local e=rollAnimation(m)runAnimTrack=l:LoadAnimation(animTable[m][e].anim)runAnimTrack.Priority=Enum.AnimationPriority.Core runAnimTrack:Play(t)if runAnimKeyframeHandler~=nil then runAnimKeyframeHandler:disconnect()end runAnimKeyframeHandler=runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)end end 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

	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 FFlagUserAdjustHumanoidRootPartToHipPosition then
			if not Humanoid.AutomaticScalingEnabled then
				return 1
			end
		end
		
		local scale = Humanoid.HipHeight / HumanoidHipHeight
		if userAnimationSpeedDampening then
			if AnimationSpeedDampeningObject == nil then
				AnimationSpeedDampeningObject = script:FindFirstChild("ScaleDampeningPercent")
			end
			if AnimationSpeedDampeningObject ~= nil then
				scale = 1 + (Humanoid.HipHeight - HumanoidHipHeight) * AnimationSpeedDampeningObject.Value / HumanoidHipHeight
			end
		end
		return scale
	end	
	return 1
end

local smallButNotZero = 0.0001
function setRunSpeed(speed)
	local speedScaled = speed * 4.5 -- Default is 1.25
	local heightScale = getHeightScale()
	local runSpeed = speedScaled / heightScale

	if runSpeed ~= currentAnimSpeed then
		if runSpeed < 0.33 then
			currentAnimTrack:AdjustWeight(1.0)		
			runAnimTrack:AdjustWeight(smallButNotZero)
		elseif runSpeed < 0.66 then
			local weight = ((runSpeed - 0.33) / 0.33)
			currentAnimTrack:AdjustWeight(1.0 - weight + smallButNotZero)
			runAnimTrack:AdjustWeight(weight + smallButNotZero)
		else
			currentAnimTrack:AdjustWeight(smallButNotZero)
			runAnimTrack:AdjustWeight(1.0)
		end
		currentAnimSpeed = runSpeed
		runAnimTrack:AdjustSpeed(runSpeed)
		currentAnimTrack:AdjustSpeed(runSpeed)
	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
			
			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

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

	-- 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
