I created a script that spawns apples in random trees inside a model. The problem is that I don't know how to make the spawn stop, since one time it will lag so much apple. Someone knows how I can stop the spawn and do it start over if you reach a specific number? For example: if there are 10 clubs on the map, the spawn stops. But if there are 9 the spawn will work again. script:
wait(game.Players.PlayerAdded)
verdade = false
applevalue = game.Workspace.appleintvalue
applevalue.Value = 0
applescripton = true
while applescripton == true do
wait(math.random(1,10))
local apple = game.Workspace.apple:Clone()
apple.Parent = workspace
local arvore = game.Workspace.arvores:GetChildren()
local applaspawn = arvore[math.random(1, #arvore)]
local tronco = applaspawn:FindFirstChild('tronco1')
apple.CFrame = tronco.CFrame * CFrame.new(math.random(-6,6),8,math.random(-6,6))
print(applaspawn)
applevalue.Value = applevalue.Value + 1
print(applevalue.Value)
end

18 comments
oh,thanks man,but whats use of 'elseif'?
It's basically an "alternative". Like, if you have two apples and you want to make something with them, but you also want to do something if there are 5 apples, you gotta either use elseif or either make unecessarelly big scripts
oh man, if i put this script inside my script, when it deactivates it will not be able to activate again, because it will not be detecting if the apples value reached 10
I meant that you have to put a script inside the main script (not the code inside your script, a script inside your script) and as it has "while true do" I'm pretty sure it's gonna detect
hey man,sorry but its not working.it dont active spawn apple script again.what do i do?
Hold up, let me check.
I edited my answer, check it
ok,wait
oh bro, this script you created will be adding and removing apples? instead of stopping and spawning again?
Well, I`m not sure how you remove the apples from your game. If you explained I could fix it
I'm thinking about: when a player clicks on them, he picks it up and it disappears from the floor, so the value of the intValue would go down
everything was working fine, the script was deactivating when the number of the intValue reached 10..the problem is that it did not activate the script again
ok ill edit this
right, what will this second script you created do?
so basically you add a click detector to a block and when the block is clicked you do something
like this man, the spawn of apples is ready now..now the only thing I wanted was for the spawn to stop if it reached 10 apples and would normally continue below that number. is there any way to do it?
it was bad bro, I didn't see your edition..wait there, I'll read
well then just copy the 9 first lines