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

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
