The loop system of clearing Coins when coins cloned most than 2500 parts

  1. What do you want to achieve? Keep it simple and clear!

ServerScript cloning the coins to workspace, but when coins cloned so much game lagging. Then i destroy coins parts from Folder.

  1. What is the issue? Include screenshots / videos if possible!

Isn’t haven’t an errors in output; but not working :slightly_frowning_face:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
local Coins = workspace.CoinGenerated:GetChildren()
local CoinsAmount = nil

while wait(0.1) do
	for i=1, #Coins do

		if CoinsAmount >= 2500 then
			Coins[i]:Destroy()
			continue
		end

		CoinsAmount += 1
		print(CoinsAmount)
	end	
end
local Coins = {}

game.Workspace.CoinGenerated.ChildAdded:Connect(function(coin)-- runs whenever a coin is added to CoinsGenerated
    if #Coins >= 2500 then
        Coins[1]:Destroy()-- destroy the first coin added to the list of coins
        table.remove(Coins, 1)-- remove the first coin from the list of coins
    end
    Coins[#Coins + 1] = coin-- add this new coin to the list of coins
end)

The reason your code doesn’t work is that you only run getchildren once at the start, and then never again. But running getchildren every .1 second for upwards of 2500 coins would lag, so this method doesn’t involve getchildren.

Thank you very much for your help, I will definitely study this code.

You not only helped me, but also made the algorithm better than I wanted!