Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to stop a While loop when an object reaches a certain size?

I am just beginning to learn coding, I am currently messing around with a block of "grass" that increases in height every 5 seconds.

While true do wait(5) game.Workspace.Part.Size=game.Workspace.Part.Size+Vector3.new(0,0.2,0) print("grass has grown") end

this works perfectly, except it keeps going on forever. What I want to learn is how to phrase the While statement to stop when the part reaches 2 studs tall. I have tried the does not equal, but I am not sure how to phrase that to check the Y value

While game.Workspace.Part.Size~=(2) do wait(5) game.Workspace.Part.Size=game.Workspace.Part.Size+Vector3.new(0,0.2,0) print("grass has grown") end

But that code doesnt work for what I need it for, and if I try

While game.Workspace.Part.Size~=(0,2,0)

it doesnt recognize it as valid.

3 answers

✓ Accepted answer

You nearly had it at the end with this: while game.Workspace.Part.Size ~= (0,2,0)

When comparing two values, they have to be of the same data type. Part.Size is a Vector3, but what you are comparing it against is just a tuple (just a list of numbers). You need to say "Compare Part.Size to this Vector3": while game.Workspace.Part.Size ~= Vector3.new(0,2,0)

While we're at it, let's do this as well:

but I am not sure how to phrase that to check the Y value

Just as parts have properties like Size and Position, Vector3s have properties too if you want to deal with specifics. In your case, if you only wanted to compare the Y value, you could use Part.Size.Y: while game.Workspace.Part.Size ~= 2 do wait(5)

Of course, whilst Part.Size is a Vector3, Part.Size.Y is just a number, so you compare it against a number, not a Vector3.

2 comments

0

My way is way simpler.

0

I mean... mine is literally an addition of the function "Vector3.new", whereas yours is a while new (and extremely flawed) system.

Why don't you use an iterative for loop? It's definitely simpler than a while loop.

local grass = workspace.Part
local goal_height = 2
local increase = 0.2
for i = grass.Size.Y, goal_size, increase do
	wait(5)
	grass.Size = Vector3.new(0, i, 0)
end
print("Grown.")

Basically, it'll loop up to your goal size. Each loop will happen five seconds after the last one. This is basically what this kind of loop was designed for, so I don't see why you wouldn't want to use it. If you're still wanting to use a while loop, it's just a simple check in the conditional:

local grass = workspace.Part
local goal_height = 2
local increase = Vector3.new(0, 0.2, 0)
while grass.Size.Y ~= goal_height do
	wait(5)
	grass.Size.Y = grass.Size + increase
end
print("Grown.")

Best of luck!

1 comment

0

I haven't gotten that far in the tutorials, I just wanted to see want I could try with the While loops. But thanks, I will try something using this!

While true do
    wait(5) 
    game.Workspace.Part.Size = game.Workspace.Part.Size+Vector3.new(0,0.2,0)  
    if game.Workspace.Part.Size == Vector3.new(0,20,0) then
         print("Grass has fully grown!")
         script:Destroy()
    end
end

What I just did there was add an if statement to see if the part size is 20 studs. And the destroy means...it will destroy the script. This script may not work as I am also new but it should.

Hope this helps you.

6 comments

0

This will work, but it's not a good way of doing it. If you had to destroy the entire script just to stop a loop, it would be nigh-impossible to get anything done.

0

Not really, it is what I would do.

0

What if you had more going on in the script? You wouldn't tend to have 1 script dedicated to 1 while loop, you'd have it as part of a larger system, which would all fail if you deleted the script when that while loop was finished.

0

But they never said that. Destroying the script was cause less lag in a game as long as there is nothing else to go on.

0

Premature optimization is generally agreed to be bad, especially when it makes your code less readable

0

Also, if you really want to talk about efficiency, you shouldn't make a bunch of scripts for each part so you can do this hacky method

This discussion is preserved as it was. New answers, comments, and votes are closed.