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 9 years ago
Player = game.Players.LocalPlayer
animation = script:WaitForChild("Animation")

-- Make variables for services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

-- Make variables for player, character, and camera
local player = game.Players.LocalPlayer
while not player.Character do wait() end
local character = player.Character
local camera = game.Workspace.CurrentCamera

-- Update camera rotation every render frame
local currentAngle = 0
local deltaAngle = 0
runService.RenderStepped:connect(function()
    currentAngle = currentAngle + deltaAngle
    camera.CoordinateFrame = CFrame.new(character.Head.Position) 
                                        * CFrame.Angles(0, math.rad(currentAngle), 0)
                                        * CFrame.new(0, 0, 10)
end)

-- Check for change event in input. Used for thumbstick input as those are analog
userInputService.InputChanged:connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.Gamepad1 then
        -- Check left thumbstick and move character on change
        if input.KeyCode == Enum.KeyCode.Thumbstick1 then
            character.Humanoid:Move(Vector3.new(input.Position.X, 0, -input.Position.Y), true)
        end
        -- Check right thumbstick and change camera angle on change
        if input.KeyCode == Enum.KeyCode.Thumbstick2 then
            deltaAngle = input.Position.X * 5
        end
    end
end)

-- Check for user input ended events. Handles release of R1 and thumbsticks
userInputService.InputEnded:connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.Gamepad1 then
        -- Stop moving character if left thumbstick released
        if input.KeyCode == Enum.KeyCode.Thumbstick1 then
            character.Humanoid:Move(Vector3.new(0,0,0))
        end
        -- Stop moving camera if right thumbstick released
        if input.KeyCode == Enum.KeyCode.Thumbstick2 then
            deltaAngle = 0
        end
        -- Make character move at normal speed if R1 is released
        if input.KeyCode == Enum.KeyCode.ButtonR1 then
            character.Humanoid.WalkSpeed = 16
        end
    end
end)

-- Check for user input began events. Handles jumping and increasing speed
userInputService.InputBegan:connect(function(input, processed)
    if input.UserInputType == Enum.UserInputType.Gamepad1 then
        -- If A button is pressed then make the character jump
        if input.KeyCode == Enum.KeyCode.ButtonA then
            character.Humanoid.Jump = true
        end
        -- If R1 is pressed then make the character move faster
        if input.KeyCode == Enum.KeyCode.ButtonR1 then
            character.Humanoid.WalkSpeed = 100
        end
        if input.KeyCode == Enum.KeyCode.ButtonX then
          local animationTrack = Player.Character.Humanoid:LoadAnimation(animation)
            animationTrack:Play()
            wait(1)
            animationTrack:Stop()
        end
    end
end)

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

1 answer

Log in to vote
1
Answered by
User#2 0
9 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:

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

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.

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

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.

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

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