Script not stopping when it reaches "Until" parameter

I’m making a block that eventually vanishes, like the disappearing tiles on Sword Fights on the Heights. I’m using a repeat-until loop to do the transparency fade, and then when the loop ends it should stop and make the block uncollidable. However, it’s continuing even after the transparency parameter is met.

while true do
	wait(1)
	repeat
		script.Parent.Transparency = script.Parent.Transparency + 0.1
		wait(0.5)
	until script.Parent.Transparency == 1
	script.Parent.CanCollide = false
	wait(5)
	script.Parent.Transparency = 0
	script.Parent.CanCollide = true
end
1 Like

I think your issue is floating point imprecision. There is no reason to reinvent the wheel though, just use a for loop.

local block = script.Parent -- use variables to avoid repetition

while true do
	wait(1)
	for i = 1, 10 do
		block.Transparency = i/10
		wait(0.5)
	end

	block.CanCollide = false
	wait(5)
	block.Transparency = 0
	block.CanCollide = true
end
5 Likes

You shouldn’t use a for a loop as it will make the code unscalable in the future, you should do something like this -

while wait(1) do

	while script.Parent.Transparency ~= 1 do
		script.Parent.Transparency += .1
		wait(.5)
	end
	
	script.Parent.CanCollide = false

	wait(5)

	script.Parent.Transparency = 0
	script.Parent.CanCollide = true
end

How is that any better? And how is a for loop less scalable? Floating point imprecision was literally the issue here.

3 Likes

If you would want to change the condition later down the line, for loops wouldn’t allow for that as easily as while loops would.

If you later wanted to keep moving a part for a certain distance, it would be easier to use while loops (This example among other things).

Simply adding a variable constant for the transparency step would make it scale no?

for i = 1, stepPercent do
	block.Transparency = i/stepPercent
	wait(0.5)
end

The only “condition” is to reach 1. For loops do the job just fine. There is literally no reason to reinvent the wheel in these cases.

1 Like

@sjr04 Still, that would mean having to change the for loop to stop at a different number each time.

@DreamWakeStudios You would then have to have a second variable to keep in mind and change whenever you need to change the condition.

While loops are made for this exact reason, do something while this is true/not true.

Same for the while loop. :slight_smile:

OP just needs to go from 0 to 1 in steps of 0.1, while/repeat until doesn’t work for this because of floating point imprecision. And for loops were quite literally made for doing something a set amount of times. This is silly.

3 Likes

By using for loops, you are hard-coding the block to go from visible to invisible. Using while loops make the code more flexible and non-rigid.

What do you even mean by that? You can also just use a variable with a for loop so you can change it.

I’m not sure what is being argued, nor how using a for loop is hard-coding, the whole point is to go from 0-1 in steps of 0.1. That is exactly how for loops are meant to work.

For example

for i = 1, 10 do
	script.Parent.Position += Vector3.new(0, 1, 0)
end

compared to

while script.Parent.Position ~= Vector3.new(0, 10, 0)
	script.Parent.Position += Vector3.new(0, 1, 0)
end

They both seem like the same thing, but the for loop is hard coded to bring the block up 10 times, the while loop will bring the block up until it is where it needs to be.

The loop ends at 10, which is the whole point, lol.

Yes? And you can just change the for loop? Also, while loops are not really intended for this type of thing. For loops are a better option.

The while loop does too, but just in a different manner.

Yes it does the same thing, but if the parts transparency doesn’t have exactly 1 decimal digit from the start, then your code would go on forever. But at the end of the day both methods do the same thing, so it doesn’t really matter which you want to use. Keep in mind to use < 1.1 instead of ~= 1 tho.

The methods that @alimm123 keeps defending were the cause of OP’s problem in the first place. Mainly due to the precision issues you and @sjr04 both pointed out, so his solution is the better option. While-loops, do-while-loops, and for-loops all have a time and place where they can be a valid solution. A for-loop, however, is the better choice for this scenario; it is best suited to fulfill the requirements OP had specified.

While loops are made to loop always, but you can break as needed.
Repeat loops are made to stop on true/false. It’s better practice to use repeat when you’re waiting for a true/false scenario.
For loops are for incrementing from X to Y, subtracting or adding Z on each step (exactly what OP is trying to do with transparency).
You are not correct.

(did not intend to reply to incapaz here, whoopsie)

@blokav couldn’t have said it better. While you have your points, I have mine. Both for and while loops are equally as important. While yes, I may have been wrong about while loops being better in the OP situation, while loops are still better in other situations.