How to make random animations play?

Hi! I have successfully scripted a script, where when a player activates a tool and clicks, an animation plays. It works GREAT!

However, however I was wondering how to play random animations, so it is different every time they click.

Here’s the script:

script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
  animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
 Animation:Play()

end)
end)

 script.Parent.Unequipped:Connect(function()
  animation:Destroy()

end)

That’s the script! Just need some help in finding out how to play random animations!

Many thanks!

8 Likes

Create a folder with the animations you have then use math.random

local animationsFolder = game.Workspace.Folder:GetChildren()

script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local randomAnimation = animationsFolder[math.random(1, #animationsFolder)]
game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(randomAnimation)
   end)
end)

 script.Parent.Unequipped:Connect(function()
  randomAnimation:Destroy()
end)

I hope I helped you :slight_smile:

12 Likes

It doesn’t seem to be working. Hmm I have typed it correctly, but nothing happens.

Do you see errors in the output?

Also make sure you changed from game.Workspace.Folder to the place of your folder

I have done so.
There’s nothing in the output about the animation.

However, there is a yellow line under

Can you make a screenshot with the script?

I have solved it I Think! Needed to add randomAnimation:Play()
And remove the local before randomAnimation

Nope, never mind. Doesn’t work.