Help how to make "while" stop when it reaches a certain point

  1. I’m trying to make a tree generator which would randomly spawn trees all across the map.

  2. The Issue is the while doesn’t stop after it passes the limit.

  3. I’ve messed around with all kinds of stuff and haven’t found an answer. I have no clue what to do.

while Value < 10 do
1 Like

Did you add 1 to Value after every time it spawns a tree?

Yes i did i made a script for it

May we see the full code that you have? It’s likely an issue with the Value variable or something else is wrong

"lua
local Folder = script.Parent.Parent.Trees

local Count = script.Parent.TreeAmount.Value

Folder.ChildAdded:Connect(function()

Count = Count + 1

print(Count)

end)

Folder.ChildRemoved:Connect(function()

Count = Count - 1

print(Count)

end)
"

while Value < 10 do
	-- Stuff
	if Value >= 10 then
		break -- this stops the loop
	end
end

I’ll assume that Value is the value of TreeAmount, the way you’re doing it only changes the Count variable, it doesn’t change the value of TreeAmount.

Change local Count = script.Parent.TreeAmount.Value to local Count = script.Parent.TreeAmount

And everytime you reference Count, you change it to Count.Value, Count.Value += 1 and Count.Value -= 1

If Value is referecing something else we need to see the While loop code

Also if Value has the same issue as Count, you’d need to fix that as well

1 Like

When childs are removed, you can use “repeat until” repeat up the count value and respawn your things until count value is at 10 for example

1 Like

It does not seem to work for me.

oops i forgot to make it a reply of your last message

I made it repeat Instead of while and it still doesn’t stop

Ive did it. I just made it repeat then placed the counter inside it

robloxapp-20210818-1641594.wmv (396.0 KB)