How to get an animation that the humanoid is playing?

  1. What do you want to achieve?
    I was making an energy charging script like Dragon Ball Rage
    using InputBegan and InputEnded

  2. What is the issue?
    after the input ends, I don’t know how to get the variable of the animation and make the animation stop

  3. What solutions have you tried so far?

server script


local function startcharge(player,maxki)
	local Character = player.Character
	local fx1 = game.ReplicatedStorage.Particles.Charge:Clone()
	local fx2 = game.ReplicatedStorage.Particles.Spread:Clone()
	fx1.Parent = Character.HumanoidRootPart
	fx2.Parent = Character.HumanoidRootPart
	local animc = script.charge:Clone()
	animc.Parent = Character.Humanoid
	local animate = Character.Humanoid:LoadAnimation(animc)
	animate:Play()
end

local function endcharge(player,maxki)
	local Character = player.Character
	Character.HumanoidRootPart.Charge:Destroy()
	Character.HumanoidRootPart.Spread:Destroy()
	Character.Humanoid.charge:Destroy()
end


game.ReplicatedStorage.Events.EnergyCharge.OnServerEvent:Connect(startcharge)
game.ReplicatedStorage.Events.StopEnergyCharge.OnServerEvent:Connect(endcharge)

local script

local Fetch = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

Fetch.InputBegan:Connect(function(inst)
	if inst.KeyCode == Enum.KeyCode.C then
		local maxki = player.leaderstats.MaximumKi
    game.ReplicatedStorage.Events.EnergyCharge:FireServer(maxki)
    end
end)


Fetch.InputEnded:Connect(function(inst)
	if inst.KeyCode == Enum.KeyCode.C then
		
		game.ReplicatedStorage.Events.StopEnergyCharge:FireServer()
		
    end
end)

Then… why not store it as a variable?:

local CurrentAnimations = {} 
--[[ 
    Creating a variable so it can be accessed by a separate function,
    this is a table so that if another person reqests a stop, it wouldn't
    stop the wrong persons animation
]]

local function startcharge(player,maxki)
	local Character = player.Character
	local fx1 = game.ReplicatedStorage.Particles.Charge:Clone()
	local fx2 = game.ReplicatedStorage.Particles.Spread:Clone()
	fx1.Parent = Character.HumanoidRootPart
	fx2.Parent = Character.HumanoidRootPart
	local animc = script.charge:Clone()
	animc.Parent = Character.Humanoid
	CurrentAnimations[Player.Name] = Character.Humanoid:LoadAnimation(animc) -- Creating the animation, then inserting it to the table
	CurrentAnimations[Player.Name]:Play() -- Playing the animation from the table
end

local function endcharge(player,maxki)
	local Character = player.Character
	Character.HumanoidRootPart.Charge:Destroy()
	Character.HumanoidRootPart.Spread:Destroy()
	Character.Humanoid.charge:Destroy()
    CurrentAnimations[Player.Name]:Stop() -- Stopping the animation from the table
    CurrentAnimations[Player.Name] = nil -- Removing from the table
end


game.ReplicatedStorage.Events.EnergyCharge.OnServerEvent:Connect(startcharge)
game.ReplicatedStorage.Events.StopEnergyCharge.OnServerEvent:Connect(endcharge)

Thanks, I did not understand how to use tables yet