Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make it pick a random Anim?

local FALL_ANIM_NEW = 'http://www.roblox.com/asset/?id=344065810'

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local animateScript = character:WaitForChild("Animate")
        local fall = animateScript:WaitForChild("fall")
        local FallAnim = fall:WaitForChild("FallAnim")
        FallAnim.AnimationId = FALL_ANIM_NEW
    end)
end)


This is a fall animation, obviously when a player falls, this animation plays.. but what if I made multiple fall animations (and I did) and I want this script to randomly pick one when a player falls?

---------------------UPDATED VERSION OF ORIGIN SCRIPT (NEED HELP ON IT)-----------------

(Not mine)

local FallAnimations = {344065810, 355981334}
local FALL_ANIM_NEW = 'http://www.roblox.com/asset/?id='

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local animateScript = character:WaitForChild("Animate")
        local fall = animateScript:WaitForChild("fall")
        local FallAnim = fall:WaitForChild("FallAnim")
        FallAnim.AnimationId = FALL_ANIM_NEW .. FallAnimations[math.random(#FallAnimations)]
    end)
end)

Now that I've added two, how come when my player falls the script automatically (all the time) chooses the second one and don't switch up?

1 comment

0

https://scriptinghelpers.org/questions/7236/what-does-do # Gives you the length of a table or string.

3 answers

✓ Accepted answer

What you could do is set up a array with the ids of all the animations you want to use. Once done with that you could then use concatenation to add the animation id to the end of the string when setting the asset link. You would have to use the math.random function to select the random number from the array.

local FallAnimations = {344065810}
local FALL_ANIM_NEW = 'http://www.roblox.com/asset/?id='

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local animateScript = character:WaitForChild("Animate")
        local fall = animateScript:WaitForChild("fall")
        local FallAnim = fall:WaitForChild("FallAnim")
        FallAnim.AnimationId = FALL_ANIM_NEW .. FallAnimations[math.random(#FallAnimations)]
    end)
end)

What the script will do is take the FALL_ANIM_NEW link and add the randomly selected id from the array using indexing on the array of a random index value. Basically meaning if the script chooses 1 the first entry in the array will be used, and if it chooses 2 then so fourth.

Hope this helped!


Edit:

local FallAnimations = {344065810}
local FALL_ANIM_NEW = 'http://www.roblox.com/asset/?id='

game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        local animateScript = character:WaitForChild("Animate")
        local fall = animateScript:WaitForChild("fall")
        local FallAnim = fall:WaitForChild("FallAnim")
	repeat wait() until character:FindFirstChild('Humanoid') --Wait until humanoid exists.
	character.Humanoid.FreeFalling:connect(function() --Establish freefalling event. Every time they go free falling, run the anonymous function.
        	FallAnim.AnimationId = FALL_ANIM_NEW .. FallAnimations[math.random(#FallAnimations)]
	end)
    end)
end)

6 comments

0

"What does the # mean in a script?" It will give you the length of a string or amount of entries in an array if they are placed in front of either. #'Hello World' would print out to be 11. #FallAnimations = {344065810} would print out to be 1.

0

I will update my original script to show you what I did

0

Was wondering if you could help fix my script change?

0

Because the script assigns the animation when the character spawns. If you reset several times you will likely see different results. Use the FreeFalling event of Humanoid to help switch up the animations.

0

Where did you learn to script? This is cool.

0

I learned off of YouTube tutorials which inspired me to do more any more thing. The videos taught me the concepts of Lua scripting to where I could go into the Roblox wiki and be able to understand the documentation.

This is easy.

Put your ID's into a table:

local fallAnims = {0000, 0000, 000, 000000}

Now you get a random number, making sure the number isn't bigger than the number of items in the table.

local num = math.random(#fallAnims)--same as: math.random(1, #fallAnims)

Now all you do is find the value in the table that corresponds to the random number you just got.

local chosenAnim = fallAnims[num]

And concatenate that into the rest of the string;

"http://www.roblox.com/asset/?id="..chosenAnim

And now you have a random animation!

You can combine most of these steps into just a few steps. I just thought it would be clearer if I separated them.

1 comment

0

What does the # mean in a script?

How about you set up a Variable for the animation play and the math.random number and make many If Statements and do something like this....

local num = math.random(1,4)
if num == 1 then
--code
end

if num == 2 then
--code 
end

if num == 3 then
--code
end

if num == 4 then
--code
end

3 comments

0

Use elseif's not multiple if statements.

0

So if I replace his "if's" with "elseif" it might work?

0

This is a bad way of accomplishing this task, although it would work.

This discussion is preserved as it was. New answers, comments, and votes are closed.