function stopAllAnimations()local q=currentAnim if emoteNames[q]~=nil and emoteNames[q]==false then q="\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 q end function getHeightScale()if Humanoid then local q=Humanoid:FindFirstChild("\066\111\100\121\072\101\105\103\104\116\083\099\097\108\101")if q and q:IsA("\078\117\109\098\101\114\086\097\108\117\101")then return q.Value end end return 1 end local q=.0001 function setRunSpeed(w)if w<.33 then currentAnimTrack:AdjustWeight(1.0)runAnimTrack:AdjustWeight(q)elseif w<.66 then local M=(w-.33)/.33 currentAnimTrack:AdjustWeight((1.0-M)+q)runAnimTrack:AdjustWeight(M+q)else currentAnimTrack:AdjustWeight(q)runAnimTrack:AdjustWeight(1.0)end local M=w*1.25 local Q=getHeightScale()runAnimTrack:AdjustSpeed(M/Q)currentAnimTrack:AdjustSpeed(M/Q)end function setAnimationSpeed(q)if q~=currentAnimSpeed then currentAnimSpeed=q if currentAnim=="\119\097\108\107"then else end end end function keyFrameReachedFunc(q)if q=="\069\110\100"then if currentAnim=="\119\097\108\107"then runAnimTrack.TimePosition=.0 currentAnimTrack.TimePosition=.0 else local q=currentAnim if emoteNames[q]~=nil and emoteNames[q]==false then q="\105\100\108\101"end local w=currentAnimSpeed playAnimation(q,.15,Humanoid)setAnimationSpeed(w)end end end function rollAnimation(q)local w=math.random(1,animTable[q].totalWeight)local M=w local Q=1 while w>animTable[q][Q].weight do w=w-animTable[q][Q].weight Q=Q+1 end return Q end function playAnimation(q,w,M)local Q=rollAnimation(q)local z=animTable[q][Q].anim if z~=currentAnimInstance then if currentAnimTrack~=nil then currentAnimTrack:Stop(w)currentAnimTrack:Destroy()end if runAnimTrack~=nil then runAnimTrack:Stop(w)runAnimTrack:Destroy()end currentAnimSpeed=1.0 currentAnimTrack=M:LoadAnimation(z)currentAnimTrack.Priority=Enum.AnimationPriority.Core currentAnimTrack:Play(w)currentAnim=q currentAnimInstance=z if currentAnimKeyframeHandler~=nil then currentAnimKeyframeHandler:disconnect()end currentAnimKeyframeHandler=currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)if q=="\119\097\108\107"then local q="\114\117\110"local Q=rollAnimation(q)runAnimTrack=M:LoadAnimation(animTable[q][Q].anim)runAnimTrack.Priority=Enum.AnimationPriority.Core runAnimTrack:Play(w)if runAnimKeyframeHandler~=nil then runAnimKeyframeHandler:disconnect()end runAnimKeyframeHandler=runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)end end end
-- functions

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
		local bodyHeightScale = Humanoid:FindFirstChild("BodyHeightScale")
		if bodyHeightScale and bodyHeightScale:IsA("NumberValue") then
			return bodyHeightScale.Value
		end
	end

	return 1
end

local smallButNotZero = 0.0001
function setRunSpeed(speed)
	if speed < 0.33 then
		currentAnimTrack:AdjustWeight(1.0)
		runAnimTrack:AdjustWeight(smallButNotZero)
	elseif speed < 0.66 then
		local weight = ((speed - 0.33) / 0.33)
		currentAnimTrack:AdjustWeight(1.0 - weight + smallButNotZero)
		runAnimTrack:AdjustWeight(weight + smallButNotZero)
	else
		currentAnimTrack:AdjustWeight(smallButNotZero)
		runAnimTrack:AdjustWeight(1.0)
	end

	local speedScaled = speed * 1.25

	local heightScale = getHeightScale()

	runAnimTrack:AdjustSpeed(speedScaled / heightScale)
	currentAnimTrack:AdjustSpeed(speedScaled / heightScale)
end

function setAnimationSpeed(speed)
	if speed ~= currentAnimSpeed then
		currentAnimSpeed = speed
		if currentAnim == "walk" then
		else
		end
	end
end

function keyFrameReachedFunc(frameName)
	if frameName == "End" then
		if currentAnim == "walk" then
			runAnimTrack.TimePosition = 0.0
			currentAnimTrack.TimePosition = 0.0
		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()
		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
