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