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
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
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
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.
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 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.
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.