Thank you for reading my question and considering helping me. To begin with I will tell you what I have been doing and what the objective is, I am using a loop to make a part grow until it reaches a maximum pre-established size and then start to decrease until it reaches a minimum pre-established size. This part of the code is working just fine.
The problematic part is stopping the loop, to do that I made a variable that needs to be true for the loop to run, so I did a touched function so that when I touched the part this variable becomes false and then the part would theoretically stop growing / decreasing, and in fact it kinda works, but the touched function waits for the loop to end for this to happen, that is, it waits until the part returns to the smallest possible size and only then stop the loop, which is not what I wanted, I want the part to stop the loop immediately when it is touched, staying at the size it was at the time of contact. What I have so far is this:
local Players = game:GetService("Players") local GrowingPart = script.Parent local grow = true local MaximumSize = Vector3.new(8, 12, 8) local MinimumSize = Vector3.new(8, 1, 8) GrowingPart.Touched:Connect(function(Hit) local Player = Players:GetPlayerFromCharacter(Hit.Parent) if Player then grow = false end end) while grow do repeat GrowingPart.Size = GrowingPart.Size + Vector3.new(0, 1, 0) GrowingPart.Position = GrowingPart.Position + Vector3.new(0, 1 / 2, 0) wait(1) until GrowingPart.Size == MaximumSize repeat GrowingPart.Size = GrowingPart.Size - Vector3.new(0, 1, 0) GrowingPart.Position = GrowingPart.Position - Vector3.new(0, 1 / 2, 0) wait(1) until GrowingPart.Size == MinimumSize end
while grow do
only ensures that the while
loop stops, you have 2 more loops which you also need to stop, to fix that for your case, you need to check in both repeat
loops if grow
is true, you would use or
operator which does what it sounds like.
... repeat GrowingPart.Size = GrowingPart.Size + Vector3.new(0, 1, 0) GrowingPart.Position = GrowingPart.Position + Vector3.new(0, 1 / 2, 0) wait(1) --/If grow becomes false, it will stop until GrowingPart.Size == MaximumSize or (grow == false) if not (grow) then --/Breaks the while loop preventing next repeat loop from --\from running, only breaks if [grow] is false break end repeat --/This is also a possible method if not (grow) then break end GrowingPart.Size = GrowingPart.Size - Vector3.new(0, 1, 0) GrowingPart.Position = GrowingPart.Position - Vector3.new(0, 1 / 2, 0) wait(1) until GrowingPart.Size == MinimumSize ...
If you still did not understand the or
operator, here is simple explanation, it checks if statement before or
or statement after it are true, if at least one of them is, it passes.
This should work however, you can use TweenService too! In my opinion it's a lot smoother and you can destroy tweens so if change your mechanics a bit, you can make an awesome script. You don't need to do, just optional.