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
4 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

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        local Animation = Instance.new("Animation") 
        Animation.AnimationId = "rbxassetid://NoSeesLol" 
        local Track = Humanoid:LoadAnimation(Animation)
        Track:Play()
    end
end)

2 answers

Log in to vote
1
Answered by
Lunaify 66
4 years ago
local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Animations = {"animationid_1","animationid_2","animationid_3","animationid_4",}

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then

        local RandomAnimation = Animations[math.random(1,#Animations)]
        local ChosenId = RandomAnimation

        print(RandomAnimation)

        local Animation = Instance.new("Animation") 
        Animation.AnimationId = ChosenId 

        local Track = Humanoid:LoadAnimation(Animation)
        Track:Play()

    end
end)

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
4 years ago

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

local UserInputService = game:GetService("UserInputService")
    local Tool = script.Parent
    local Player = game.Players.LocalPlayer
    local Character = Player.Character
    local Humanoid = Character:WaitForChild("Humanoid")

    UserInputService.InputBegan:Connect(function(InputObject)
        if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
local numberofanims = 3 --put how many animations you have
local num = math.random(1,numberofanims)
if num == 1 then
            local Animation = Instance.new("Animation")
            Animation.AnimationId = "rbxassetid://NoSeesLol"
            local Track = Humanoid:LoadAnimation(Animation)
            Track:Play()
elseif num == 2 then
            local Animation = Instance.new("Animation")
            Animation.AnimationId = "rbxassetid://NoSeesLol"
            local Track = Humanoid:LoadAnimation(Animation)
            Track:Play()
elseif num == 3 then
            local Animation = Instance.new("Animation")
            Animation.AnimationId = "rbxassetid://NoSeesLol"
            local Track = Humanoid:LoadAnimation(Animation)
            Track:Play()
end
        end
    end)

Answer this question