Making a loop "un-break"

So let’s say for example:

I have a script

local Apple = 0

while wait(1) do
  Apple += 1
  if Apple > 5
      print('more than 5 apples!')
      workspace.AppleSounds.FiveApples:Play()
      break
elseif Apple > 10
      print('Around 10 apples or more!')
      workspace.AppleSounds.TenApples:Play()
      break
end
end
end

And when the Apple counter has reached 5, it will print and play a little jingle

It will break to not spam the jingle sound

But since it’s broken now, it will not add a Apple to the Apples counter.
Is there a way to make a loop un-break?

1 Like

Try not to use loops to detect changes, as complications may appear. Instead use events like the one in the NumberValue instance.

local NumberValue = game.Workspace.Value --apples are stored in here

NumberValue.Changed:Connect(function(NewValue) --fires anytime the Value of NumberValue changes
	if NewValue = 5 then    --do know that NumberValue.Value and NewValue are the same number past this point
		print('more than 5 apples!')
		workspace.AppleSounds.FiveApples:Play()
	elseif NewValue = 10 then   
		print('Around 10 apples or more!')
		workspace.AppleSounds.TenApples:Play()
	end
end)	
	
while wait(1) do
	NumberValue.Value += 1
end

-- the event helps seperate adding apples and checking apples, making them more flexible in the future

but if you wanted to use the loop, add a simple equal operation to the if statements

local Apple = 0

while wait(1) do
	Apple += 1
	if Apple == 5 then --dont know why you put a ">" symbol, but it was the problem
		print('more than 5 apples!')
		workspace.AppleSounds.FiveApples:Play()
	elseif Apple == 10 then
		print('Around 10 apples or more!')
		workspace.AppleSounds.TenApples:Play()
	end
end

hope this helps :grin:

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.