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

How to play one of 3 random animations with tool when clicking?

I created 3 animations and tool (sword) and now I want it to be like when you click with mouse it randomly chooses and plays one of these animations. (my scripting skills are very low) my current script:

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local randomAnimation = math.random(1,3)
if randomAnimation == 1 then
local animation = script.Parent:FindFirstChild("Animation1")
local swingAnimation = character.Humanoid:LoadAnimation(animation)
local canSwing = true
local debounce = 0.5
script.Parent.Activated:Connect(function()
	if canSwing then
		canSwing = false
		swingAnimation:play()
		wait(debounce)
		canSwing = true
	end
end)
	elseif randomAnimation == 2 then
local animation = script.Parent:FindFirstChild("Animation2")
local swingAnimation = character.Humanoid:LoadAnimation(animation)
local canSwing = true
local debounce = 0.5
script.Parent.Activated:Connect(function()
	if canSwing then
		canSwing = false
		swingAnimation:play()
		wait(debounce)
		canSwing = true
	end
  end)
	
elseif randomAnimation == 3 then
local animation = script.Parent:FindFirstChild("Animation3")
local swingAnimation = character.Humanoid:LoadAnimation(animation)
local canSwing = true
local debounce = 0.5
script.Parent.Activated:Connect(function()
	if canSwing then
		canSwing = false
		swingAnimation:play()
		wait(debounce)
		canSwing = true
	end
  end)
end

1 answer

✓ Accepted answer

this should hopefully do the trick

local player = game:GetService("Players").LocalPlayer
repeat wait() until player.Character and player.Character:FindFirstChild('Humanoid')
--wait for character to load in
local character,humanoid = player.Character,player.Character.Humanoid

local Track1,Track2,Track3 = Instance.new('Animation'),Instance.new('Animation'),Instance.new('Animation')
Track1.AnimationId = 'rbxassetid://123'
Track2.AnimationId = 'rbxassetid://456'
Track3.AnimationId = 'rbxassetid://789'
--creates 3 animation tracks (put ur own ID)
local Load1,Load2,Load3 = humanoid:LoadAnimation(Track1),humanoid:LoadAnimation(Track2),humanoid:LoadAnimation(Track3)
--loads the 3 animations
local AnimTable = {Load1,Load2,Load3}
--puts the animations in a table to use for later

local Tool = script.Parent

local Debounce = false
Tool.Activated:Connect(function()
	if Debounce == false then
		Debounce = true
		AnimTable[math.random(1,#AnimTable)]:Play()
		--picks a random anim from the table and plays it
		wait(.5)
		Debounce = false
	end
end)

3 comments

0

Thank you, this really helped me!

0

np! since it helped, i would appreciate if you accepted the answer.

1

sorry but I don't know how, when I will found out I will 100% do it.

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