So I have several animations I can use for my combat system.
How can I make it so it will use several different attacks instead of just one?
example:
https://gyazo.com/02e13be2bb7c95c82ffc9eded18890d3
Current code:
local tool = script.Parent
local canDamage = false
local canSwing = true
local Handle = script.Parent:WaitForChild("Handle")
local player = game.Players.LocalPlayer
local Character = player.Character or player.PlayerAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
Sounds = {
Slash = Handle:WaitForChild("SlashSound"),
Lunge = Handle:WaitForChild("LungeSound"),
Unsheath = Handle:WaitForChild("EquipSound"),
HitSound1 = Handle:WaitForChild("HitSound1")
}
AttackAnimationsList = {
"rbxassetid://9404404634",
"rbxassetid://9404407784"
}
local LoadedAnimation = {}
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if not humanoid then
return
end
if humanoid.Parent ~= tool.Parent and canDamage then
humanoid:TakeDamage(5)
Sounds.HitSound1:Play()
else
return
end
canDamage = false
end
local function slash()
local Character = tool.Parent
local Humanoid = Character.Humanoid
local Sword_Slash_Animation = Instance.new("Animation")
Sword_Slash_Animation.AnimationId = "rbxassetid://9404404634"
local AnimationTrack = Humanoid:LoadAnimation(Sword_Slash_Animation)
if canSwing then
canSwing = false
AnimationTrack:Play()
Sounds.Slash:Play()
canDamage = true
wait(1)
canSwing = true
end
end
local Sword_Walk_Animation = Instance.new("Animation")
Sword_Walk_Animation.AnimationId = "rbxassetid://9404399317"
local AnimationTrackWalk = Humanoid.Animator:LoadAnimation(Sword_Walk_Animation)
local Sword_Idle_Animation = Instance.new("Animation")
Sword_Idle_Animation.AnimationId = "rbxassetid://9404394687"
local AnimationTrackIdle = Humanoid.Animator:LoadAnimation(Sword_Idle_Animation)
local Sword_Equip_Animation = Instance.new("Animation")
Sword_Equip_Animation.AnimationId = "rbxassetid://9537941896"
local AnimationTrackEquip = Humanoid:LoadAnimation(Sword_Equip_Animation)
local shouldPlay = false
tool.Equipped:Connect(function()
shouldPlay = true
end)
Sounds.Unsheath:Play()
AnimationTrackEquip:Play()
while shouldPlay do
wait()
Humanoid.Running:Connect(function(speed)
if speed > 0 then
AnimationTrackWalk:Play()
AnimationTrackIdle:Stop()
else
AnimationTrackWalk:Stop()
AnimationTrackIdle:Play()
end
end)
end
tool.Unequipped:Connect(function()
shouldPlay = false
AnimationTrackIdle:Stop()
AnimationTrackWalk:Stop()
end)
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)
Hi!
Just take your random table with attacks to choose from, and pick a random one to load when they “attack”
AttackAnimationsList = {
"rbxassetid://9404404634",
"rbxassetid://9404407784"
}
local RandomAttack = AttackAnimationsList[math.random(#AttackAnimationsList)]
2 Likes
How can I call this?
I’ve tried RandomAttack:Play()
I also redid the code a bit as
local Sword_Attack1_Animation = Instance.new("Animation")
Sword_Attack1_Animation.AnimationId = "rbxassetid://9404404634"
local AnimationTrackAttack1 = Humanoid.Animator:LoadAnimation(Sword_Attack1_Animation)
local Sword_Attack2_Animation = Instance.new("Animation")
Sword_Attack2_Animation.AnimationId = "rbxassetid://9404407784"
local AnimationTrackAttack2 = Humanoid.Animator:LoadAnimation(Sword_Attack2_Animation)
AttackAnimationsList = {
AnimationTrackAttack1,
AnimationTrackAttack2
}
local RandomAttack = AttackAnimationsList[math.random(#AttackAnimationsList)]
Hi!
What is the error when you run your code?
If you want it in order instead if random, try this
local Sword_Attack1_Animation = Instance.new("Animation")
Sword_Attack1_Animation.AnimationId = "rbxassetid://9404404634"
local AnimationTrackAttack1 = Humanoid.Animator:LoadAnimation(Sword_Attack1_Animation)
local Sword_Attack2_Animation = Instance.new("Animation")
Sword_Attack2_Animation.AnimationId = "rbxassetid://9404407784"
local AnimationTrackAttack2 = Humanoid.Animator:LoadAnimation(Sword_Attack2_Animation)
local CurrentAnimation = 1
local AnimationsTable = {
["Animation1"] = AnimationTrackAttack1,
["Animation2"] = AnimationTrackAttack2
}
--when attacking do
AnimationsTable["Animation" .. CurrentAnimation]:Play()
CurrentAnimation += 1
if CurrentAnimation > #AnimationsTable then
CurrentAnimation = 1
end
if CurrentAnimation = 3 then
CurrentAnimation = 1
end
Should be
if CurrentAnimation > #AnimationsTable then
CurrentAnimation = 1
end
1 Like
Thank you, will change that now 
Also we could remove the “Animation” part, and turn it into a index.
local AnimationsTable = {
[1] = AnimationTrackAttack1,
[2] = AnimationTrackAttack2
}
local Index = 1
if AnimationsTable[Index] then
AnimationsTable[Index]:Play()
Index += 1
else
Index = 1
AnimationsTable[Index]:Play()
end
Tbh I don’t see the need for this part, it just seems like extra lines, but I agree with the part above that
Okay I used your code (sorry if i’m being difficult here, i do not exactly understand much)
Though it only plays the Attack1 animation.
It’s used here
local function slash()
local Character = tool.Parent
local Humanoid = Character.Humanoid
local Sword_Slash_Animation = Instance.new("Animation")
Sword_Slash_Animation.AnimationId = "rbxassetid://9404404634"
local AnimationTrack = Humanoid:LoadAnimation(Sword_Slash_Animation)
if canSwing then
AnimationsTable["Animation" .. CurrentAnimation]:Play()
CurrentAnimation += 1
if CurrentAnimation > #AnimationsTable then
CurrentAnimation = 1
canSwing = false
Sounds.Slash:Play()
canDamage = true
wait(1)
canSwing = true
end
end
end
Is this because of the wait(1)?
Where is AnimationsTable defined? Please show full script if possible.
local tool = script.Parent
local canDamage = false
local canSwing = true
local Handle = script.Parent:WaitForChild("Handle")
local player = game.Players.LocalPlayer
local Character = player.Character or player.Character:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Sword_Walk_Animation = Instance.new("Animation")
Sword_Walk_Animation.AnimationId = "rbxassetid://9404399317"
local AnimationTrackWalk = Humanoid.Animator:LoadAnimation(Sword_Walk_Animation)
local Sword_Idle_Animation = Instance.new("Animation")
Sword_Idle_Animation.AnimationId = "rbxassetid://9404394687"
local AnimationTrackIdle = Humanoid.Animator:LoadAnimation(Sword_Idle_Animation)
local Sword_Equip_Animation = Instance.new("Animation")
Sword_Equip_Animation.AnimationId = "rbxassetid://9537941896"
local AnimationTrackEquip = Humanoid:LoadAnimation(Sword_Equip_Animation)
local shouldPlay = false
Sounds = {
Slash = Handle:WaitForChild("SlashSound"),
Lunge = Handle:WaitForChild("LungeSound"),
Unsheath = Handle:WaitForChild("EquipSound"),
HitSound1 = Handle:WaitForChild("HitSound1")
}
local Sword_Attack1_Animation = Instance.new("Animation")
Sword_Attack1_Animation.AnimationId = "rbxassetid://9404404634"
local AnimationTrackAttack1 = Humanoid.Animator:LoadAnimation(Sword_Attack1_Animation)
local Sword_Attack2_Animation = Instance.new("Animation")
Sword_Attack2_Animation.AnimationId = "rbxassetid://9404407784"
local AnimationTrackAttack2 = Humanoid.Animator:LoadAnimation(Sword_Attack2_Animation)
local CurrentAnimation = 1
local AnimationsTable = {
["Animation1"] = AnimationTrackAttack1,
["Animation2"] = AnimationTrackAttack2
}
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
if not humanoid then
return
end
if humanoid.Parent ~= tool.Parent and canDamage then
humanoid:TakeDamage(5)
Sounds.HitSound1:Play()
else
return
end
canDamage = false
end
local function slash()
local Character = tool.Parent
local Humanoid = Character.Humanoid
local Sword_Slash_Animation = Instance.new("Animation")
Sword_Slash_Animation.AnimationId = "rbxassetid://9404404634"
local AnimationTrack = Humanoid:LoadAnimation(Sword_Slash_Animation)
if canSwing then
AnimationsTable["Animation" .. CurrentAnimation]:Play()
CurrentAnimation += 1
if CurrentAnimation > #AnimationsTable then
CurrentAnimation = 1
canSwing = false
Sounds.Slash:Play()
canDamage = true
wait(1)
canSwing = true
end
end
end
tool.Equipped:Connect(function()
shouldPlay = true
Sounds.Unsheath:Play()
AnimationTrackEquip:Play()
while shouldPlay do
wait()
Humanoid.Running:Connect(function(speed)
if tool.Parent == player.Backpack then return end
if speed > 0 then
AnimationTrackWalk:Play()
AnimationTrackIdle:Stop()
else
AnimationTrackWalk:Stop()
AnimationTrackIdle:Play()
end
end)
end
end)
local shouldPlay = false
tool.Unequipped:Connect(function()
shouldPlay = false
AnimationTrackIdle:Stop()
AnimationTrackWalk:Stop()
end)
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)
local function slash()
if canSwing then
AnimationsTable["Animation" .. CurrentAnimation]:Play()
CurrentAnimation += 1
if CurrentAnimation > #AnimationsTable then
CurrentAnimation = 1
end
canSwing = false
Sounds.Slash:Play()
canDamage = true
wait(1)
canSwing = true
end
end
Replace your slash() function with this, and tell me if it works
It still only does the one animation. Let me check with the animator to see if they are slightly different or something.
Edit: They both are different. Must be a code problem them.
Are you still able to help me with this or?
I’ve managed to get it to work actually, i used the indexing, so i’m testing to see if it can do three now.