Breaking a loop Does not Work!

When I made a loop in a Script, It Does not Work!

This is my script:

local EndStorm = workspace.Storm.EndStorm

while true do
	wait()
	if script.Parent.Size == EndStorm.Size then
		break
	end
	
	script.Parent.Size = script.Parent.Size - Vector3.new(2,0,2)
end

print("Storm Stopped Shrinking.")

When It is meant to Stop at the Right Size it Just Keeps on Shrinking!

Try this:

local EndStorm = workspace.Storm.EndStorm

while task.wait() do
	if script.Parent.Size.X <= EndStorm.Size.X and script.Parent.Size.Z <= EndStorm.Size.Z then
		break
	end

	script.Parent.Size = script.Parent.Size - Vector3.new(2,0,2)
end

print("Storm Stopped Shrinking.")
1 Like

Try return. If that does not work then try repeat until

1 Like

I’ll elaborate/explain what the updated code in the post that was marked as a solution does in case anyone is unsure of what the differences between the updated code and the original code are and why it worked


In the original post, the conditional statement is checking if the size of script.Parent is equal to the exact same value as EndStorm.Size to make sure the loop doesn’t continue shrinking the size of script.Parent.

For each iteration of the loop where those two Instances are not the exact same size, the X and Z values of script.Parent's Size property are subtracted by two.

This causes an issue under certain circumstances where the sizes of the two Instances will never be the same:

Example Values

  • script.Parent.Size == Vector3.new(10, 0, 10)
  • EndStorm.Size == Vector3.new(7, 0, 7)

Using the example values above, the size of script.Parent would never be exactly the same as EndStorm.Size because it would become Vector3.new(8, 0, 8) and then Vector3.new(6, 0, 6), skipping over Vector3.new(7, 0, 7) because 2 studs were being subtracted from the X and Z axes each iteration of the loop.


However, this can be solved by modifying the conditional statement to check if the size of script.Parent is less than or equal to the size of the EndStorm Instance, which is what was suggested in the solution/first response to this topic:

if
    script.Parent.Size.X <= EndStorm.Size.X -- Checks if script.Parent's size value on the X axis is less than or equal to EndStorm's size value on the X Axis
    and script.Parent.Size.Z <= EndStorm.Size.Z -- Checks if script.Parent's size value on the Z axis is less than or equal to EndStorm's size value on the Z Axis
then
    break -- If both of those conditions were met, then the loop no longer needs to continue reducing the size of script.Parent since it's reached/exceeded the intended threshold
3 Likes