I want to check when the animation stops so I can set the debounce or in this case canAttack to true
but when I run the game and press MouseButton1 to attack this error shows up:
I tried using animationTrack.Stopped:Wait() and animationTrack.Stopped:Connect()
and this is the script
-- {{ SERVICES }} --
local cas = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
-- RemoteEvent instances
local hitboxRemote = game.ReplicatedStorage:WaitForChild("HitboxEvent")
-- Animations folder
local anims = game.ReplicatedStorage:WaitForChild("Animations")
-- Get player
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
-- Weapon stats table (temporary)
local weaponStats = {
Name = "Fists",
Damage = 10,
MaxCombo = 4,
Range = 1,
Attackspeed = 0.85,
Endlag = 0.1
}
-- Ensure that the character's humanoid contains an "Animator" object
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animateScript = character:WaitForChild("Animate")
local WeaponAnimations = game.ReplicatedStorage:WaitForChild("Animations"):WaitForChild(weaponStats.Name)
-- Variables for combo and combo reset
local attack = 1
local lastAttack = 1
local comboWindow = 0.5
-- Debounce variable
local canAttack = true
-- Equipped variable
local equipped = false
local function setweaponanims()
animateScript.idle.Animation1.AnimationId = WeaponAnimations.Idle.AnimationId
animateScript.idle.Animation1.Weight.Value = 10
animateScript.walk.WalkAnim.AnimationId = WeaponAnimations.Walk.AnimationId
--animateScript.run.RunAnim.AnimationId = WeaponAnimations.Run
end
local function setDefaultAnimations()
animateScript.idle.Animation1.AnimationId = "http://www.roblox.com/asset/?id=180435571"
animateScript.idle.Animation1.Weight.Value = 10
animateScript.walk.WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=180426354"
--animateScript.run.RunAnim.AnimationId = "run.xml"
end
local function Equip(actionName, inputState, InputObject)
if inputState == Enum.UserInputState.End then return end
-- load weapon equip animation
local equipAnim = WeaponAnimations:WaitForChild("Equip")
if not equipped then
setweaponanims()
equipped = true
return
end
setDefaultAnimations()
equipped = false
end
local function Attack(actionName, inputState, InputObject)
if not canAttack then return end
if not equipped then return end
if inputState == Enum.UserInputState.End then return end
if tick() - lastAttack > 1.5 then
attack = 1
end
if attack < weaponStats.MaxCombo then
canAttack = false
local animationTrack = animator:LoadAnimation(WeaponAnimations:WaitForChild("Attack" ..attack))
animationTrack:AdjustSpeed(weaponStats.Attackspeed)
animationTrack:Play()
animationTrack:GetMarkerReachedSignal("Hitbox"):Connect(function()
hitboxRemote:FireServer(weaponStats)
end)
animationTrack.Stopped():Wait()
wait(weaponStats.Endlag)
canAttack = true
attack += 1
else
canAttack = false
local animationTrack = animator:loadAnimation(WeaponAnimations.Flourish)
animationTrack:Play()
animationTrack:GetMarkerReachedSignal("Hitbox"):Connect(function()
hitboxRemote:FireServer(weaponStats, 1, 0.35)
end)
animationTrack.Stopped():Wait()
wait(comboWindow)
canAttack = true
attack = 1
end
lastAttack = tick()
end
-- Assign Binds
cas:BindAction("Equip", Equip, true, Enum.KeyCode.E)
cas:BindAction("Attack", Attack, true, Enum.UserInputType.MouseButton1)