Errors in a Sword Combat System

Hello. I’m working on a Sword Combat System and got some animation errors, they aren’t playing how I expected, like repeating same combo animations and doing things I didn’t animate even with the correct priority.

--Parts that matter
function TestSword:CheckComboReset(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	if not Character then return end

	local combo = Character:GetAttribute("Combo") or 1
	
	if not LastAttackTime[Player.UserId] then
		LastAttackTime[Player.UserId] = 0
	end
	
	local CurrentTime = time()
	if CurrentTime - LastAttackTime[Player.UserId] > COMBO_RESET_TIME then
		if combo > 1 then
			Character:SetAttribute("Combo", 1)
		end
	end
end

function TestSword:M1(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	if not Character then return end
	local Humanoid = Character:FindFirstChild("Humanoid")
	if not Humanoid then return end

	local CurrentTime = time()
	
	LastAttackTime[Player.UserId] = CurrentTime
	
	local combo = Character:GetAttribute("Combo") or 1

	if combo >= 4 then
		Character:SetAttribute("Combo", 1)
	else
		Character:SetAttribute("Combo", combo + 1)
	end
	
	local AttackAnimation = Instance.new("Animation")
	
	AttackAnimation.AnimationId = Animations[Character:GetAttribute("Combo") + 1] --[1] = idle
	local AttackTrack = Humanoid:LoadAnimation(AttackAnimation)
	AttackTrack.Priority = Enum.AnimationPriority.Action
	AttackTrack:Play()

	AttackTrack.Stopped:Connect(function()
		AttackAnimation:Destroy()
	end)
	
	local Arggs = {
		Character = Character,
		Combo = Character:GetAttribute("Combo")
	}
	
	TestSword.SlashEffect(Arggs)
	
	local animationDuration = AttackTrack.Length
	CooldownEvent:FireClient(Player, "M1", animationDuration)
	--CameraShaker:ShakeOnce(2, 4, 0.2, 0.3)

	end

What is happening (ignore laggy slashes, just care about animations):

What it was supposed to do:

1 Like

Check
the
Prioroty

of the animation. set it to action. update the animation. now see.

My animations were set to Action3 when I published them to Roblox and my code, as you can see, set their priority to Action, and I already tried to set them to another priorities by scripts, but nothing happened…

hmm maybe another one of your animations is the same priotory like idle or soemthing

I noticed that I didn’t put another important part of the code, that is the idle animation, I guess that could cause conflict with another animations.

function TestSword:PlayIdleAnimation(Character)
	local Humanoid = Character:FindFirstChild("Humanoid")
	if not Humanoid then return end

	if not Character:FindFirstChild("IdleAnimationTrack") then
		local IdleAnimation = Instance.new("Animation", Character)
		IdleAnimation.AnimationId = Animations[1] -- Idle Animation ID
		local AnimationTrack = Humanoid:LoadAnimation(IdleAnimation)
		AnimationTrack.Priority = Enum.AnimationPriority.Idle
		AnimationTrack.Name = "IdleAnimationTrack"
		AnimationTrack:Play()

		Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			local speed = Humanoid.MoveDirection.Magnitude
			if speed > 0 then
				AnimationTrack:Stop()
			else
				AnimationTrack:Play()
			end
		end)
	end
end

function TestSword:StopIdleAnimation(Character)
	local IdleTrack = Character:FindFirstChild("IdleAnimationTrack")
	if IdleTrack then
		IdleTrack:Stop()
		IdleTrack:Destroy()
	end
	local IdleAnimation = Character:FindFirstChild("IdleAnimation")
	if IdleAnimation then
		IdleAnimation:Destroy()
	end
end
1 Like

i had this exact same problem what I did was set the action priority of the swings to be higher than the idle animation. You should double check that.

Another thing, you do not need to stop and play the idle animation when you play the swinging animations

1 Like

set your idle animation priority to action and swing animations to something higher like action 2

1 Like

I think the problem lies here:

local combo = Character:GetAttribute("Combo") or 1

if combo >= 4 then
	Character:SetAttribute("Combo", 1)
else
	Character:SetAttribute("Combo", combo + 1)
end

if 1 is idle, then first you’re setting it to one. Then you’re adding one, now combo is 2. However, you are playing animation combo + 1. Which is 3. I think is supposed to be 2.

If this is not the answer, try seeing if the server side is setting combo correctly.

They are set now to Action3 but nothing changed

I’m adding 1 because the first item in my array Animations is the idle animation, so it adds 1 to get the slash animations

Yes, I fixed that recently, as you can see in the video idle animation was wrong, it was playing even with the sword unequiped, but the swing aimation problem is still there…

My new code:

function TestSword:M1(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	if not Character then return end
	local Humanoid = Character:FindFirstChild("Humanoid")
	if not Humanoid then return end

	local CurrentTime = time()
	
	LastAttackTime[Player.UserId] = CurrentTime
	
	local combo = Character:GetAttribute("Combo") or 0

	if combo >= 4 then
		Character:SetAttribute("Combo", 1)
	else
		Character:SetAttribute("Combo", combo + 1)
	end
	
	local AttackAnimation = Instance.new("Animation")
	if combo ~= 0 then
		if combo == 1 then
			AttackAnimation.AnimationId = SwingAnimations[1]
		elseif combo == 2 then
			AttackAnimation.AnimationId = SwingAnimations[2]
		elseif combo == 3 then
			AttackAnimation.AnimationId = SwingAnimations[3]
		elseif combo == 4 then
			AttackAnimation.AnimationId = SwingAnimations[4]
		end
	end
	
	local AttackTrack = Humanoid:LoadAnimation(AttackAnimation)
	AttackTrack.Priority = Enum.AnimationPriority.Action2
	AttackTrack:Play()
	print("Playing animation: "..AttackAnimation.AnimationId.." for combo: "..combo)

	AttackTrack.Stopped:Connect(function()
		AttackAnimation:Destroy()
	end)
	
	local Arggs = {
		Character = Character,
		Combo = Character:GetAttribute("Combo")
	}
	
	TestSword.SlashEffect(Arggs)
	
	local animationDuration = AttackTrack.Length
	CooldownEvent:FireClient(Player, "M1", animationDuration)
end

Well, the same animation is playing for some reason, even with the log print("Playing animation: "..AttackAnimation.AnimationId.." for combo: "..combo) showing them doing everything correctly. And the animations doesn’t seem to play correctly, even with Action2 or Action3, like, the player don’t execute the animation well and there is not anything else in conflict because I don’t have another animations playing in this place.

Actually, I solved this. there were some animation problems when exporting and some combo logic that I changed. Thanks for the community help!