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

How to play one of 3 random animations with tool when clicking?

Asked by 4 years ago
Edited 4 years ago

I created 3 animations and tool (sword) and now I want it to be like when you click with mouse it randomly chooses and plays one of these animations. (my scripting skills are very low) my current script:

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local randomAnimation = math.random(1,3)
if randomAnimation == 1 then
local animation = script.Parent:FindFirstChild("Animation1")
local swingAnimation = character.Humanoid:LoadAnimation(animation)
local canSwing = true
local debounce = 0.5
script.Parent.Activated:Connect(function()
    if canSwing then
        canSwing = false
        swingAnimation:play()
        wait(debounce)
        canSwing = true
    end
end)
    elseif randomAnimation == 2 then
local animation = script.Parent:FindFirstChild("Animation2")
local swingAnimation = character.Humanoid:LoadAnimation(animation)
local canSwing = true
local debounce = 0.5
script.Parent.Activated:Connect(function()
    if canSwing then
        canSwing = false
        swingAnimation:play()
        wait(debounce)
        canSwing = true
    end
  end)

elseif randomAnimation == 3 then
local animation = script.Parent:FindFirstChild("Animation3")
local swingAnimation = character.Humanoid:LoadAnimation(animation)
local canSwing = true
local debounce = 0.5
script.Parent.Activated:Connect(function()
    if canSwing then
        canSwing = false
        swingAnimation:play()
        wait(debounce)
        canSwing = true
    end
  end)
end

1 answer

Log in to vote
1
Answered by
Mayk728 855 Moderation Voter
4 years ago
Edited 4 years ago

this should hopefully do the trick

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character and player.Character:FindFirstChild('Humanoid')
--wait for character to load in
local character,humanoid = player.Character,player.Character.Humanoid

local Track1,Track2,Track3 = Instance.new('Animation'),Instance.new('Animation'),Instance.new('Animation')
Track1.AnimationId = 'rbxassetid://123'
Track2.AnimationId = 'rbxassetid://456'
Track3.AnimationId = 'rbxassetid://789'
--creates 3 animation tracks (put ur own ID)
local Load1,Load2,Load3 = humanoid:LoadAnimation(Track1),humanoid:LoadAnimation(Track2),humanoid:LoadAnimation(Track3)
--loads the 3 animations
local AnimTable = {Load1,Load2,Load3}
--puts the animations in a table to use for later

local Tool = script.Parent

local Debounce = false
Tool.Activated:Connect(function()
    if Debounce == false then
        Debounce = true
        AnimTable[math.random(1,#AnimTable)]:Play()
        --picks a random anim from the table and plays it
        wait(.5)
        Debounce = false
    end
end)
0
Thank you, this really helped me! datunalaitadze 3 — 4y
0
np! since it helped, i would appreciate if you accepted the answer. Mayk728 855 — 4y
1
sorry but I don't know how, when I will found out I will 100% do it. datunalaitadze 3 — 4y
Ad

Answer this question