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

How to play random animations?

Asked by 10 years ago
01Player = game.Players.LocalPlayer
02animation = script:WaitForChild("Animation")
03 
04-- Make variables for services
05local userInputService = game:GetService("UserInputService")
06local runService = game:GetService("RunService")
07 
08-- Make variables for player, character, and camera
09local player = game.Players.LocalPlayer
10while not player.Character do wait() end
11local character = player.Character
12local camera = game.Workspace.CurrentCamera
13 
14-- Update camera rotation every render frame
15local currentAngle = 0
View all 74 lines...

Obviously this is an edit of the xbox controller code, I'm just making it do different things. I wanna know is how to make it to where when someone presses x multiple times it well, 50/50 chance of it being 1 out of two animations.

0
use the math.random in cases like this. woodengop 1134 — 10y
1
....That simple? Wow I should now this stuff man. kingalpha1 15 — 10y
1
How would I implement math.random in this situation? kingalpha1 15 — 10y
0
the way you have your script made, makes it hard to explain. woodengop 1134 — 10y
View all comments (3 more)
1
Mind talking to me on roblox about it? kingalpha1 15 — 10y
0
can't party you :l woodengop 1134 — 10y
1
Should be able to now kingalpha1 15 — 10y

1 answer

Log in to vote
1
Answered by
User#2 0
10 years ago

I only saw one animation in your script, so I can't really directly help you, though I can offer this information:

With anything random in lua, you'll probably want to use math.random! math.random is quite useful, and theres a few ways to use it.

If you just call math.random() then you'll get a number between 0 and 1, obviously a decimal! Here are some examples of numbers I got when calling math.random with no parameters:

1for i = 1, 5 do
2    print(math.random())
3end

0.47254860072634 0.79055146946623 0.85747856074709 0.48014770958586 0.98303170873135

Your next option is to provide one parameter, a number! This will generate a number between 1 and that number. This is called the "upper bound." The "lower bound," in this case, is 1.

1for i = 1, 5 do
2    print(math.random(5))
3end

1 3 2 2 1

And finally, you can supply two parameters, a lower and upper bound (in that order). The upper bound must be greater than (it can be equal to, but you'll just get the same number) the lower bound.

1for i = 1, 5 do
2    print(math.random(5, 10))
3end

5 5 9 9 5

Note: If you try to use a number with a decimal as either an upper or lower bound, it'll be rounded away from 0 (1.5 -> 2, -1.5 -> -2).

With this knowledge, we know there's 2 ways to get a 50% chance.

  • You can use math.random() and check if the number it returns is greater than or equal to 0.5, aka 50%
  • You can use math.random(1, 2) and if the number returned is 1 then play the first animation, if the number returned is 2 then play the second.
Ad

Answer this question