-- Waits for the child of the specified parent


local function WaitForChild(parent, childName)
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end

local Tool = script.Parent

local Animations = {}
local MyHumanoid
local MyCharacter


local function PlayAnimation(animationName)
	if Animations[animationName] then
		Animations[animationName]:Play()
	end
end

local function StopAnimation(animationName)
	if Animations[animationName] then
		Animations[animationName]:Stop()
	end
end


function OnEquipped(mouse)
	MyCharacter = Tool.Parent
	MyHumanoid = WaitForChild(MyCharacter, 'Humanoid')
	if MyHumanoid then
		Animations['EquipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'EquipAnim5'))
		Animations['IdleAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'IdleAnim3'))
		Animations['OverheadAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'OverheadAnim2'))
		Animations['SlashAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'SlashAnim2'))
		Animations['ThrustAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'ThrustAnim2'))
		Animations['UnequipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'UnequipAnim2'))
	end
mouse.KeyDown:connect(function(key)
function Taunt()
	--gather all of the humanoids in a 30stud radius
	local torsos = {}
	for _, p in pairs(game.Players:GetChildren()) do
		if p ~= game.Players:GetPlayerFromCharacter(Tool.Parent) then
			if p.Character and p.Character:FindFirstChild('Torso') then
				torsos[#torsos+1] = p.Character.Torso
			end
		end
	end

	--now pull them in towards us
	local mpos = Tool.Parent.Torso.Position
	for _, torso in pairs(torsos) do
		if (mpos-torso.Position).magnitude < 40 then
			local dir = (mpos-torso.Position).unit
			------------ stolen from dagger of time to make a character "fly" in some direction
				local force = Instance.new("BodyVelocity")
				force.velocity = Vector3.new(0,1,0)
				force.Parent = torso
				torso.Velocity = dir*250
				game.Debris:AddItem(force, 0.5)
			------------
		end
	end
end

function rTaunt()
	--gather all of the humanoids in a 30stud radius
	local torsos = {}
	for _, p in pairs(game.Players:GetChildren()) do
		if p ~= game.Players:GetPlayerFromCharacter(Tool.Parent) then
			if p.Character and p.Character:FindFirstChild('Torso') then
				torsos[#torsos+1] = p.Character.Torso
			end
		end
	end

	--now pull them in towards us
	local mpos = Tool.Parent.Torso.Position
	for _, torso in pairs(torsos) do
		if (mpos-torso.Position).magnitude < 40 then
			local dir = (mpos-torso.Position).unit
			------------ stolen from dagger of time to make a character "fly" in some direction
				local force = Instance.new("BodyVelocity")
				force.velocity = Vector3.new(0,1,0)
				force.Parent = torso
				torso.Velocity = -dir*250
				game.Debris:AddItem(force, 0.5)
			------------
		end
	end
end

	if key == 'q' then
		if debounce==nil then
			debounce=true
	Animations['EquipAnim']:Play(.1,.8,2)
			Taunt()
			wait(5)
			debounce=nil
		end
	elseif key == 'e' then
		if debounce2==nil then
			debounce2=true
	Animations['EquipAnim']:Play(.1,.8,2)
			rTaunt()
			wait(5)
			debounce2=nil
		end
	end
end)


	Animations['EquipAnim']:Play(.1,.8,1)
	PlayAnimation('IdleAnim')
end

function OnUnequipped()
	Tool.Handle.deactivate:Play()
	for animName, _ in pairs(Animations) do
		StopAnimation(animName)
	end
end

Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)

WaitForChild(Tool, 'PlaySlash').Changed:connect(
	function (value)
		--if value then
			PlayAnimation('SlashAnim')
		--else
		--	StopAnimation('SlashAnim')
		--end
	end)

WaitForChild(Tool, 'PlayThrust').Changed:connect(
	function (value)
		--if value then
			PlayAnimation('ThrustAnim')
		--else
		--	StopAnimation('ThrustAnim')
		--end
	end)

WaitForChild(Tool, 'PlayOverhead').Changed:connect(
	function (value)
		--if value then
			Animations['OverheadAnim']:Play()
		--else
		--	StopAnimation('OverheadAnim')
		--end
	end)