Disconnecting a function while a wait() is running?

My problem is pretty simple, I’m trying to disconnect/stop a function while a wait() code is running under a function in a spawn() code. This is made to reset a combo of a melee combat system. It kind of works, however if the player clicks like every 0.5 seconds, the combo resets and the player can keep spamming and never end the combo, making it an ‘exploit’. This is my current code:

    Remote.OnClientEvent:Connect(function(code,CD)
    	if code == "Light" then
    		spawn(function()
    			wait(CD + 1)
    			if Character:FindFirstChild("LightAttack") == nil then
    				lightCount = 1
    			end	
    		end)
    	end
    end)

I want it to stop the spawn(function() when a new “LightAttack” child is added to the character, so it stops counting to reset the combo. How would I go about it? I tried this:

    Remote.OnClientEvent:Connect(function(code,CD)
    	if code == "Light" then
    		spawn(function()
                Character.ChildAdded:Connect(function(child)
                   if child.Name == "LightAttack" then
                      return
                   end
                end
    			wait(CD + 1)
    			if Character:FindFirstChild("LightAttack") == nil then
    				lightCount = 1
    			end	
    		end)
    	end
    end)

But nothing changes, as the ChildAdded event is, of course, being ignored or it’s simply returning itself. What’s a possible solution to this?

Character.ChildAdded:Connect(function(child)
    if child.Name == "LightAttack" then
        return
     end
end

Is just returning itself.

You could simply add a bool value here, then before you add another light attack, check to see if this bool value has been changed.

If that’s the entirety of the code for the OnClientEvent function, spawn will be unnecessary. Internally I think roblox already creates a new thread so you won’t have to worry about spawn.

No idea what you are trying to do but give this one a try.

    local ok = 0
    Character.ChildAdded:Connect(function(child)
         if child.Name == "LightAttack" then
              ok+=1
         end
    end
    Remote.OnClientEvent:Connect(function(code,CD)
    	if code == "Light" then
                local myok = ok
    			wait(CD + 1)
    			if ok==myok and Character:FindFirstChild("LightAttack") == nil then
    				lightCount = 1
    			end	
    		end)
    	end
    end)