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

How do I have alternate animations for a hotkey?

Asked by
Lyphios 77
5 years ago

So currently I have some code that plays an animation when mousebutton1 is down, but I want it to have alternate animations. I want it to be randomized, so that each time I press the mouse button it plays a different animation. Here's my code

01local UserInputService = game:GetService("UserInputService")
02local Tool = script.Parent
03local Player = game.Players.LocalPlayer
04local Character = Player.Character
05local Humanoid = Character:WaitForChild("Humanoid")
06 
07UserInputService.InputBegan:Connect(function(InputObject)
08    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
09        local Animation = Instance.new("Animation")
10        Animation.AnimationId = "rbxassetid://NoSeesLol"
11        local Track = Humanoid:LoadAnimation(Animation)
12        Track:Play()
13    end
14end)

2 answers

Log in to vote
1
Answered by
Lunaify 66
5 years ago
01local UserInputService = game:GetService("UserInputService")
02local Tool = script.Parent
03local Player = game.Players.LocalPlayer
04local Character = Player.Character
05local Humanoid = Character:WaitForChild("Humanoid")
06 
07local Animations = {"animationid_1","animationid_2","animationid_3","animationid_4",}
08 
09UserInputService.InputBegan:Connect(function(InputObject)
10    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
11 
12        local RandomAnimation = Animations[math.random(1,#Animations)]
13        local ChosenId = RandomAnimation
14 
15        print(RandomAnimation)
View all 24 lines...

i think this works, just make sure to add the animation id's in the "Animation" table.

Ad
Log in to vote
0
Answered by
niroqeo 123
5 years ago

I'm on my phone so I can't test it.

01local UserInputService = game:GetService("UserInputService")
02    local Tool = script.Parent
03    local Player = game.Players.LocalPlayer
04    local Character = Player.Character
05    local Humanoid = Character:WaitForChild("Humanoid")
06 
07    UserInputService.InputBegan:Connect(function(InputObject)
08        if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
09local numberofanims = 3 --put how many animations you have
10local num = math.random(1,numberofanims)
11if num == 1 then
12            local Animation = Instance.new("Animation")
13            Animation.AnimationId = "rbxassetid://NoSeesLol"
14            local Track = Humanoid:LoadAnimation(Animation)
15            Track:Play()
View all 28 lines...

Answer this question