Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by 4 years ago

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

Log in to vote
1
Answered by
crywink 419 Moderation Voter
4 years ago

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.

0
Thank you so much for answering this but can you explain the enabled and alternate boolean to me ( I'm still quite confused) FireCoolflame 21 — 4y
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. crywink 419 — 4y
0
For the alternate bool: It's the same thing you had. The term for it among programmers is "debounce" though. crywink 419 — 4y
Ad

Answer this question