Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I have alternate animations for a hotkey?

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

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.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.