A play animation tool

Hi i’m wondering how i could make this play the first animation then click again to play a second animation?

-- local player = game.Players.LocalPlayer
local character
local humanoid
local inputService = game:GetService("UserInputService")
local tool = script.Parent
local Attack
local Equipped = false

tool.Equipped:Connect(function()
	character = player.Character
	humanoid = character.Humanoid
	if not Attack then
		Attack = humanoid:LoadAnimation(tool:WaitForChild("Attack1"))
	end
	Equipped = true
		wait(.1)
end)
tool.Activated:Connect(function()
	if Attack and not Attack.IsPlaying then
		Attack:Play()
		--Delete the next 3 lines if you don't want the char to be frozen in place while attacking
		character.PrimaryPart.Anchored = true
		wait(Attack.Length*0.98)
		character.PrimaryPart.Anchored = false
	end
end)
local function unequip()
	if Equipped then
		Equipped = false
		Idle:Stop()
	end
end

tool.Unequipped:Connect(unequip)

Some recommendations:

  • Use events instead of wait(). Events are way more accurate and often handle things like animations ending early.
  • Use a variable to store the state. In this case I create a boolean called onActivatedPlayAttack1 to determine which attack plays. This variable is switched when an attack plays to alternate the attacks
local player = game.Players.LocalPlayer --I uncommented this because you used the player below ... you could get the player from the character also
local character
local humanoid
local inputService = game:GetService("UserInputService")
local tool = script.Parent
local Attack1Track
local Attack2Track
local Equipped = false

tool.Equipped:Connect(function()
	character = player.Character
	humanoid = character.Humanoid
	Attack1Track = humanoid:LoadAnimation(tool:WaitForChild("Attack1"))
	Attack2Track = humanoid:LoadAnimation(tool:WaitForChild("Attack2")) --add your second animation named "Attack2"
	Equipped = true
	--wait(.1)--this doesn't do anything because it's at the end of this function. 
end)

local onActivatedPlayAttack1 = true --Use a state that you can flip to decide which animation to play
tool.Activated:Connect(function()
	if not (Attack1Track.IsPlaying or Attack2Track.IsPlaying) then
		local currentTrack
		if onActivatedPlayAttack1 then
			currentTrack = Attack1Track
			onActivatedPlayAttack1 = false
		else
			currentTrack = Attack2Track
			onActivatedPlayAttack1 = true
		end
		currentTrack:Play()
		character.PrimaryPart.Anchored = true
		currentTrack.Stopped:Wait()
		character.PrimaryPart.Anchored = false
	end
end)

local function unequip()
	if Equipped then
		Equipped = false
		Idle:Stop() --I assume this is part of code in a different section
	end
end

tool.Unequipped:Connect(unequip)
1 Like