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.
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.
math.random()
and check if the number it returns is greater than or equal to 0.5, aka 50%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.