Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make it so that if I clicked again the second animation would play?

What do I do if I want to make it so that if I clicked again it would play my second animation that shows the left arm punching

local player = game.Players.LocalPlayer local mouse = player:GetMouse() local animation = script:WaitForChild("Animation1") enabled = true

mouse.Button1Down:Connect(function() if enabled then enabled = false

	local animationTrack = player.Character.Humanoid:LoadAnimation(animation)
	animationTrack:Play()
	
	wait(1)
	enabled = true
end

end)

1 answer

✓ Accepted answer

Hey!

So I'm assuming your goal here is to have alternating punch animations. To do this, we'll just have a boolean (we'll call Alternate) that switches every punch. On every punch, we'll just change Alternate to its opposite.

Code shown below...

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Animation1 = Player.Character.Humanoid:LoadAnimation(script:WaitForChild("Animation1")) -- Instead of loading them every punch, we can just do it once :)
local Animation2 = Player.Character.Humanoid:LoadAnimation(script:WaitForChild("Animation2"))

local Alternate = false -- This will be our alternating variable
local Enabled = true -- This is actually called a debounce.

Mouse.Button1Down:Connect (function()
	if Enabled then
		Enabled = false
		
		if Alternate then
			Animation1:Play()
		else
			Animation2:Play()
		end
		
		Alternate = not Alternate -- Changes to it's opposite value (true -> false / false -> true)
		wait(1)
		Enabled = true
	end
end)

If this helped, please upvote it so I can get credit for taking the time to answer your question.

3 comments

0

Thank you so much for answering this but can you explain the enabled and alternate boolean to me ( I'm still quite confused)

0

For the enabled bool: all we're doing is switching the boolean from true to false and from false to true every time the mouse is clicked.

0

For the alternate bool: It's the same thing you had. The term for it among programmers is "debounce" though.

This discussion is preserved as it was. New answers, comments, and votes are closed.