function stopAllAnimations()local e=currentAnim 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 e end function setAnimationSpeed(e)if e~=currentAnimSpeed then currentAnimSpeed=e currentAnimTrack:AdjustSpeed(currentAnimSpeed)end end function keyFrameReachedFunc(e)if e=="\069\110\100"then if currentAnim=="\119\097\108\107"then runAnimTrack.TimePosition=.0 currentAnimTrack.TimePosition=.0 else local e=currentAnim local S=currentAnimSpeed playAnimation(e,.15,Humanoid)setAnimationSpeed(S)end end end function rollAnimation(e)local S=math.random(1,animTable[e].totalWeight)local c=S local Y=1 while S>animTable[e][Y].weight do S=S-animTable[e][Y].weight Y=Y+1 end return Y end function playAnimation(e,S,c)local Y=rollAnimation(e)local R=animTable[e][Y].anim if R~=currentAnimInstance then if currentAnimTrack~=nil then currentAnimTrack:Stop(S)currentAnimTrack:Destroy()end if runAnimTrack~=nil then runAnimTrack:Stop(S)runAnimTrack:Destroy()end currentAnimSpeed=1.0 currentAnimTrack=c:LoadAnimation(R)currentAnimTrack:Play(S)currentAnim=e currentAnimInstance=R if currentAnimKeyframeHandler~=nil then currentAnimKeyframeHandler:disconnect()end currentAnimKeyframeHandler=currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)end end


function stopAllAnimations()
	local oldAnim = currentAnim
	
	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 setAnimationSpeed(speed)
	if speed ~= currentAnimSpeed then
		currentAnimSpeed = speed
		currentAnimTrack:AdjustSpeed(currentAnimSpeed)
	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
			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)
		 
		-- 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)
	end
end