Stopping an if and returning to loop


As the title suggests, I need to know how to basically stop the rest of the if loop once a requirement is met, but I don’t know how to right now. The script is as follows:

local Point1 = script.Parent:WaitForChild("Point1")
local Point2 = script.Parent:WaitForChild("Point2")
local CommonChest = game.Workspace.CommonChest
local CommonChestBody = CommonChest.Body
local CommonChestHead = CommonChest.Head
local Pos1 = Point1.Position
local Pos2 = Point2.Position
local X1 = Pos1.X
local Y1 = Pos1.Y
local Z1 = Pos1.Z
local X2 = Pos2.X
local Y2 = Pos2.Y
local Z2 = Pos2.Z
while true do
	--Get random coordinates
	local xRand = math.random(math.min(X1, X2), math.max(X1, X2))
	local yRand = math.random(math.min(Y1, Y2), math.max(Y1, Y2))
	local zRand = math.random(math.min(Z1, Z2), math.max(Z1, Z2))
	if math.random(1,10) <= 5 then
		CommonClone = CommonChest:Clone()
		print("Successfully created a Common Chest!")
	if math.random(5,10) <= 7 then
			print("Successfully created a Rare Chest!")
			if math.random(8,10) <= 9 then
				print("Successfully created an Epic Chest!")
				print ("Successfully created a legendary chest!")
	local waittime = math.random(1,5)

Any help will be greatly appreciated.

You can use return and it will stop execution past that point and return back to the beginning.
You should probably wait at the beginning though since it would create crates extremely fast in this case.

1 Like

Ok, I’ll let you know how it goes. Thanks,

And also quick question: can you destroy clones outside of the command they were given in? Ex. in an if loop.

I’m unsure what your question is.
You can probably add them to a table and destroy them at later date.
Or delay the destroy function/add to debris.

I want the chests to clone themselves and fall to the ground, but when the waittime runs out, I want it to be destroyed, but that doesn’t work, and I get an error in the output.

You should do something like:

local Chest = ... -- Spawned chest
delay(20, function()
  pcall(game.Destroy, Chest)
1 Like