Help on making random animation

Ok so my ideal is like this

local randomAnimID = {123, 456, 789}

When the character is idle then do a Instance.new(“Animation”) then the script will choose which animation id to the Animation Instance then after the Anim length the current anim stop then Anim Instance get new random animid again.
I dont know how to do that, currently new to script. Please give me some suggest. Thanks for reading :raised_hand_with_fingers_splayed::+1:

1 Like

Does the player choose the animation or is it automatically randomly picked?

The script choose fhe random animid

You can use math.Random to choose a random animation.

An example on how to use Math.Random.

print(math.random(1,6)) -- returns a number between 1 and 6

So you would need to make sure the animation id can be accessed possibly by using api or just trial and error (there is probably another way but I haven’t got time to test right now) then you can do math.random(1,9) for the length the animation id or you could math.random(1,animation id length)

Then you would store those in a variable and make a new Animation with
Instance.new(‘Animation’) and setting the Animation id to the number.

1 Like

I don’t have time to fully test right now but I will tomorrow so if nobody else answers I will probably have one

i knew about math.random but after the Anim Length does the Animation Instance get a new animid again? I havent try to not tick “Looped” on the anim , but if i do that will the animInstane get a new id or it still play the old one

Im on my phone rn just thinking so tomorrow i will prob try something

You’d have to add a new animation id for the animation id for another animation play.

Inside a local script under StarterCharacterScripts

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
repeat wait() until Player.Character

local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animations = Character:WaitForChild("Animations") -- Wherever your list of animation instances are.

local AnimationsArray = { }

for i, Animation in ipairs(Animations:GetChildren()) do
	local AnimationTrack = Humanoid.Animator:LoadAnimation(Animation)
	table.insert(AnimationsArray, AnimationTrack)
end

function PlayRandomAnimation()
	local RandomAnimation = math.random(1, #AnimationsArray)
	local AnimationTrack = AnimationsArray[RandomAnimation]
	AnimationTrack:Play()
	AnimationTrack.Stopped:Wait()
	PlayRandomAnimation()
end

PlayRandomAnimation()
3 Likes