I created a script that generates apples on trees. I also created an extra function that stops the spawn of apples if it reaches ten apples on the map. Okay so far ... the problem is that the function that checks how many apples on the map is not activating, does anyone know why? script:
wait(game.Players.PlayerAdded)
verdade = false
applevalue = game.Workspace.appleintvalue
applevalue.Value = 0
applescripton = true
function applecheck ()
while true do
wait(0.5)
print('checando')
if applevalue.Value ~= 3 then
applescripton = true
end
if applevalue.Value == 3 then
applescripton = false
end
end
end
function spawnapple ()
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
end
spawnapple()
applecheck()
if you find any errors, could you explain to me why this is happening and give me some tips on?

4 comments
Don't use spawn. Use coroutine.wrap(spawnapple)()
^^ Note, coroutine can be hard to explain to new programmers, especially ones such as the one asking the question, there is no real problem in using spawn(). I find it quite fine to use both spawn() and coroutine.wrap() if I'm not worried about time.
the more you use spawn for your functions, the longer it takes to run
oh thanks programmerhere and sebgamingkid