How could i fire this remote better, so that it doesnt fire multiple times? (ik why it does)


UIS.InputBegan:Connect(function(input,gpe)
    if gpe then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if mob and isAttacking == false and canAttack == true and canRetreat == false then
                AttackGui.Attack.Text = "RETREAT"
            for i,pets in pairs(equippedPets:GetChildren()) do
                pets.Parent = sentPets
            end
            
             SendPets = RunService.RenderStepped:Connect(function()
                if mob.Parent ~= nil then
                    isAttacking = true
                    canRetreat = true
                    positionPets(mob.Parent, sentPets)
                end
            end)
            
            for i,fighter in pairs(sentPets:GetChildren()) do
                local anim = fighter:WaitForChild("Animations").Punch
                local fighterHum = fighter:WaitForChild("Humanoid")
                local punch = fighterHum:LoadAnimation(anim)
                punch:Play()
                AttackEvent:FireServer(fighter,mob) --THIS ONE
            end
        end
        
        if mob and isAttacking == true and canAttack and canRetreat == true then
            AttackGui.Attack.Text = "ATTACK"
            isAttacking = false
            canRetreat = false
            for i,pets in pairs(sentPets:GetChildren()) do
                pets.Parent = equippedPets
            end
            
            local RetreatPets = RunService.RenderStepped:Connect(function()
                if mob.Parent ~= nil then
                    positionPets(player.Character, equippedPets)
                end
            end)
            
            for i,fighter in pairs(equippedPets:GetChildren()) do
                RetreatEvent:FireServer(fighter,mob) --THIS ONE
            end
            
            if SendPets then
                SendPets:Disconnect()
            end
        end
    end
end)

look in the script i showed where the problems are also the animation doesnt play and idk why
ik why its firing multiple times and i think ik a fix but i also want to know why the animations arent playing

Pretty sure you need to load the animation to the animator instead of directly to the humanoid.

local punch = fighterHum.Animator:LoadAnimation(anim)

Maybe add a debounce or something.

doesnt change anything but thanks for trying

Try moving

local punch = fighterHum:LoadAnimation(anim)

outside of the function and add

if not punch.IsPlaying then
	punch:Play()
end

Also, in terms of optimising the remove events, simply send the table of equipped pets:

RetreatEvent:FireServer(fighter, equippedPets:GetChildren())

Then, handle the retreating action for each index of the table.