How to create random animation script?

Hello me and my friend have been working on a script that plays any amount of animations i can make and play them all randomly after each on finishes
we havent gotten it to work so im looking for help
here is the script

local animation = script:WaitForChild('Animation')
local humanoid = script.Parent:WaitForChild('Humanoid')
local animations = {"1", "2", "3", "4"}
animation = Instance.new("Animation",  script.Parent.Parent.Parent)
animation.AnimationID = "http://www.roblox.com/asset?id="..animations[math.random(1, #animations)]
AT = script.Parent.Parent.Parent.Humanoid:LoadAnimation(animation)
AT:Play()
1 Like

Your animation IDs aren’t 1, 2, 3, and 4. You’d have to replace 1, 2, 3, and 4 with your actual IDs for the animations. Also, Humanoid:LoadAnimation is deprecated, use the new Animator.

1 Like

yes i know that im just looking for help before we do it
also is where a way we could add more animation slots or another way to add more?

Animation slots? I’m not sure I understand, you’ll have to explain a little further.

like a way to add more animations to it
like i wanted it to play 10 animations randomly

Just add more to the array that you’re already using to store IDs.

so like
{“1”, “2”, “3”, “4”,“5” and more}

1 Like

You could just do…
nextAnimationId = list[math.random(1, #list)]

But if the animations play a part in some sort of mechanic or skill (e.g. sword fighting), you may wish to add some consistency with sequential animation selection.

For example. If I click once, the first animation appears.
If I click again in a certain amount of time, the second animation appears, etc.

If I don’t click the mouse, the sequence resets so the first animation will appear next click.

The game S4 League used this to great affect with its sword mechanics, and something similar is seen in games like Counter Strike.

i just want a npc to play a random animation without and inputs like a click just on its own to play and animation and when it ends a different one plays

while true do
local animation = script:WaitForChild('Animation')
local humanoid = script.Parent:WaitForChild('Humanoid')
local animations = {"1", "2", "3", "4"}
animation = Instance.new("Animation",  script.Parent.Parent.Parent)
animation.AnimationID = "http://www.roblox.com/asset?id="..animations[math.random(1, #animations)]
AT = script.Parent.Parent.Parent.Humanoid:LoadAnimation(animation)
AT:Play()
AT.Stopped:Wait()

end

Wait for the Stopped event of the animation track. Also, please indent that if you use it: I can’t on mobile.

Written on mobile

and all i do is simply put the animation in 1 2 3 and 4?

Put the animation ids.

where do i put my animation id

while true do
	local animation = script:WaitForChild('Animation')
	local humanoid = script.Parent:WaitForChild('Humanoid')
	local animations = {"6087331463", "6087338968", "6087342407", "6087344954"}
	animation = Instance.new("Animation",  script.Parent.Parent.Parent)
	animation.AnimationID = "http://www.roblox.com/asset?id="..animations[math.random(1, #animations)]
	AT = script.Parent.Parent.Parent.Humanoid:LoadAnimation(animation)
	AT:Play()
	AT.Stopped:Wait()

end

it doesnt play

11:42:22.314 Infinite yield possible on 'Workspace.Dummy.Script:WaitForChild(“Animation”)

its properly rigged

Try this @Troublemaiker

local npc = script.Parent
local humanoid = npc:FindFirstChild("Humanoid") 													-- I presume the NPC does have a Humanoid?
local animations = { 6087331463, 6087338968, 6087342407, 6087344954} 					-- Animation Ids are integers, not strings, so you don't need the " symbol.
local animator = humanoid:FindFirstChild("Animator",true) or Instance.new("Animator",humanoid)		-- If the Animator object isn't found in the NPC, create a new one.
local animation = animator:FindFirstChild("Animation") or Instance.new("Animation",animator)		-- Create an animation object for the Animator to use

--Main Loop
while humanoid.Health > 0 do 											-- Its not a good idea to create an infinite loop, so this loop can be stopped by killing the NPC.
	local animationId = animations[math.random(1,#animations)]			-- Get a random animation from the animations list
	animation.AnimationId = "http://www.roblox.com/asset/?id="..animationId
	
	local animationTrack = animator:LoadAnimation(animation)			-- The track represents what is currently playing.
	animationTrack:Play()												-- Play the track
	animationTrack.Stopped:Wait()										-- Wait until the track is Stoppped
	wait(0.5)
	print("Next Animation")
end

alright man thanks for your time