How to make my script randomizes the damage animation?

Hello everyone! im sorry if this sounds a little dumb, But how do i make my script has 2 damage animations instead of one and randomizes them? here is my script

local Humanoid = script.Parent.Humanoid

local Anim = Instance.new('Animation') 
Anim.AnimationId = 'rbxassetid://9061440582' 

repeat wait() until Humanoid:IsDescendantOf(workspace)
local AnimationTrack = Humanoid:LoadAnimation(Anim)
AnimationTrack.Priority = Enum.AnimationPriority.Action
local LastKnownHealth
game:GetService('RunService').RenderStepped:Connect(function()
	if LastKnownHealth then
		if LastKnownHealth > Humanoid.Health then 
			print('Damaged')
			AnimationTrack:Play()
		end
	end

	LastKnownHealth = Humanoid.Health
end)

Im sorry again if this sounds dumb but im kinda new to scripting.

1 Like

local animation = instance.new(“Animation”)

local Animations = {

[1] = —your animation id
[2] = — your animation id 
}
local RandomAnimationID = Animations[math.random(1#RandomAnimationID)]

animation.AnimationId = RandomAnimationID


This script uses a Table which will have your animation ids. Then, it will choose a random one. After that, it will insert the Id into the animation so then you have a random id

1 Like
local Humanoid = script.Parent.Humanoid

local RandomAnims = {"rbxassetid://9061440582","rbxassetid://youranimidhere"}
local Anim = Instance.new('Animation') 

repeat wait() until Humanoid:IsDescendantOf(workspace)

local RandomAnimId = RandomAnims[math.random(1,#RandomAnims)]
Anim.AnimationId = RandomAnimId
local AnimationTrack = Humanoid:LoadAnimation(Anim)
local LastKnownHealth
game:GetService('RunService').RenderStepped:Connect(function()
	if LastKnownHealth then
		if LastKnownHealth > Humanoid.Health then 
			print('Damaged')
			AnimationTrack:Play()
		end
	end

	LastKnownHealth = Humanoid.Health
end)

This script shows only 1 damage animation on damage…

This is the best way to accomplish what you would like to have. Tables are the most effective way to conjure random values.

1 Like

Did you put your 2nd animation id in “rbxassetid://youranimidhere”?

Use this:

local Humanoid = script.Parent.Humanoid

local Anim = Instance.new('Animation') 
Anim.AnimationId = 'rbxassetid://9061440582' 
local Anim2 = Instance.new('Animation') 
Anim2.AnimationId = 'rbxassetid://secondid' 

repeat wait() until Humanoid:IsDescendantOf(workspace)
local AnimationTracks = {
	Humanoid:LoadAnimation(Anim),
	Humanoid:LoadAnimation(Anim2)
}
for _, anim in pairs(AnimationTracks) do
	anim.Priority = Enum.AnimationPriority.Action
end
local LastKnownHealth
game:GetService('RunService').RenderStepped:Connect(function()
	if LastKnownHealth then
		if LastKnownHealth > Humanoid.Health then 
			print('Damaged')
			AnimationTracks[math.random(1, 2)]:Play()
		end
	end

	LastKnownHealth = Humanoid.Health
end)

Yes but it only shows the first animation when damaged