Animations and CurrentAttacks value not working

I’ve been trying to make a combat for a really long time and i can’t get the combos to work it’ll always break and just keeps on making the current attack value 2 instead of starting back at 1 or using the wrong animation when getting to 4 using the 3rd animation instead of the one it was intended for

local module = {}

local RP = game:GetService(“ReplicatedStorage”)
local StarterPlayer = game:GetService(“StarterPlayer”)
local HitService = require(RP.Modules.HitService)
local HitBoxClass = require(RP.Modules.HitboxClass)
local HitboxTypes = require(RP.Modules.HitboxClass.Types)

local MaxCombo = 4
local Last = tick()
local N = math.huge

local function FindHumanoid(Player: Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild(“Humanoid”)
return Humanoid
end

local function StopAnim(Character: Model)
local Humanoid = Character:FindFirstChild(“Humanoid”)
for i,v in pairs(Humanoid.Animator:GetPlayingAnimationTracks()) do
if v.Name ~= “Idle” and v.Name ~= “Animaiton” then
v:Stop()
end
end
end

local function ChangeCombo(Character: Model)
local CurrentAttacks = Character:FindFirstChild(“Values”):FindFirstChild(“CurrentAttacks”)

Last = tick()

if CurrentAttacks.Value == 4 then
CurrentAttacks.Value = 1
else
CurrentAttacks.Value += 1
end
end

local function GetAnim(Character:Model, AnimationName)
local CurrentAttacks = Character:FindFirstChild(“Values”):FindFirstChild(“CurrentAttacks”)
local CurrentAnim = RP.Animations.HeroicDragon.M1[“Attack”…CurrentAttacks.Value]
return CurrentAnim
end

function module.Start(Player: Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = FindHumanoid(Player)
local RootPart = Character:FindFirstChild(“HumanoidRootPart”)
local RootParts = Character.PrimaryPart
local Animator = Humanoid:FindFirstChild(“Animator”)

local Values = Character:FindFirstChild(“Values”)
local CurrentAttacks = Values:FindFirstChild(“CurrentAttacks”)
local Swinging = Values:FindFirstChild(“Swinging”)
local Attacking = Values:FindFirstChild(“Attacking”)
local Animation = Animator:LoadAnimation(GetAnim(Character, CurrentAttacks.Value))

local hitboxParams = {
SizeOrPart = Vector3.new(5.5,6,6,8),
Blacklist = {Character},
DebounceTime = 1,
InitialPosition = RootPart.CFrame + RootPart.CFrame.LookVector,
VelocityPrediction = true,
Debug = true,
} :: HitboxTypes.HitboxParams

local HitBox, connected = HitBoxClass.new(hitboxParams)
HitBox:WeldTo(RootPart, CFrame.new(0,0,-4))
HitBox:SetVelocityPrediction(false)

HitBox.HitSomeone:Connect(function(HitChars)
for _, v in HitChars do
if v and v.Parent then
local HUM = v:FindFirstChildOfClass(“Humanoid”)
if HUM then
local T

  			if v:IsA("Model") then
  				T = v.PrimaryPart.Position
  			else v:IsA("BasePart")
  				T = v.Position
  			end

  			if T then
  				local LV = Instance.new("LinearVelocity", RootPart)
  				local Attachment0 = Instance.new("Attachment", RootPart)
  				LV.ForceLimitMode = Enum.ForceLimitMode.PerAxis
  				LV.MaxForce = math.huge
  				LV.Attachment0 = Attachment0
  				LV.VectorVelocity = (RootPart.Position - T).Unit * N
  				game.Debris:AddItem(LV,0.2)
  				game.Debris:AddItem(Attachment0,0.2)
  			end
  			if CurrentAttacks.Value >= MaxCombo then
  				HitService.Hit(HUM,5/2,0.7,RootPart.CFrame.LookVector*20,nil)
  			else
  				HitService.Hit(HUM,5/2,0.7,RootPart.CFrame.LookVector*5,nil)
  			end
  		end
  	end
  end

end)

if tick() - Last > 1 then
CurrentAttacks.Value = 1
end

if Attacking.Value == true then return end

Swinging.Value = true
Attacking.Value = true

StopAnim(Character)
Animation:Play()
ChangeCombo(Character)

Humanoid.WalkSpeed = 1
Humanoid.JumpHeight = 0

Animation:GetMarkerReachedSignal(“HitStart”):Connect(function()
HitBox:Start()
end)

Animation:GetMarkerReachedSignal(“HitEnd”):Connect(function()
HitBox:Destroy()

  Swinging.Value = false
  if CurrentAttacks.Value == MaxCombo then
  	task.wait(.7)
  else
  	task.wait(.1)
  end

  Attacking.Value = false

end)

Animation.Stopped:Connect(function()
HitBox:Destroy()

  if Swinging.Value ~= true then
  	Humanoid.WalkSpeed = StarterPlayer.CharacterWalkSpeed
  	Humanoid.JumpHeight = StarterPlayer.CharacterJumpHeight
  end

end)

end

return module

right so, this is a basic diagnosis without being able to test.

  1. first you load the animation before you reset the combo:
local Animation = Animator:LoadAnimation( GetAnim(Character) )
-- …
if tick() - Last > 1 then
  CurrentAttacks.Value = 1
end

So if you pause for more than a second, you do reset CurrentAttacks.Value to 1, but after you’ve already chosen which animation to load. That means the very first attack after a long pause still grabs whatever CurrentAttacks.Value was left at (e.g. 4), then you bump it to 2 immediately in ChangeCombo. You never actually play Attack 1, you always jump to Attack 2.

  1. you bump your combo counter (ChangeCombo) immediately on start

That means by the time the next button press happens, you’re already ahead, like you’re off by one. When 4 becomes 1, you’ll end up loading the wrong animation clip.

  1. The wrap-around reset happens too early

Inside of ChangeCombo:

if CurrentAttacks.Value == 4 then
  CurrentAttacks.Value = 1
else
  CurrentAttacks.Value += 1
end
Last = tick()

That works when you go 1-2-3-4-1 but because you’re incrementing right when you start rather than when the animation finishes (or when the next input comes, whichever is first), its easy to get into states where your “current” value isn’t actually what you think it is

some fixes:

  1. Check for a slow-combo reset before you pick the animation
  2. When you do pick an animaiton, pick it based on the combo value
  3. Only increment or wrap your combo counter after you’ve queued up the animation, ideally at the end of the attack (your ‘HitEnd’ marker)

Thanks for the help i really really appreciate it i finally got it to work after doing what you said and Thank god too because i’ve been stuck on this for weeks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.