Hello! I made a script which spawns in Candy that the player can collect, however I feel that to avoid too many spawning there should be a cap to how many there can be at one time. However, since I am still pretty new to scripting I need some help. How could I add this system to my script? When the Candy is spawned, it is placed inside a folder called “CandyG” and I thought the children of this folder could be counted and that is possibly how it could function? Anyways tell me your ideas.
local coin = game.Workspace.CandyPart
local counter = 0
local CandyCap = 250 --The max amount of Candy that can spawn
function SpawnCandy()
local newcoin = coin:Clone()
newcoin.Parent = game.Workspace.CandyG
newcoin.Position = Vector3.new(math.random(-138, 125.895), 5.268, math.random(21.053, 286.874))
end
while true do
if counter > 120 then --This spams candy at the start of the game so when a player joins an empty server there is still a good amount of candy
wait(math.random(0.2,0.5))
else
wait()
end
counter += 1
SpawnCandy()
end