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

My repeat loop doesn't stop after the X number decreases to the intended value?

Hello guys. I have a script that "stops" working after decreasing to the intended value (0 "zero"), but it doesn't stop working after reaching it.

Here's the repeat loop:

local x = 8.5
repeat wait(0.2)
	script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(x,0,0)
x -= 0.1
until x == 0
script.Disabled=true

I think my problem starts after the "until" because it doesn't stop after reaching the intended value. I tried putting that, didn't work, as usual...

until x == 0
script.Disabled=true

What should I type instead to make it work?

5 comments

0

I don't know what's wrong, but I wouldn't suggest using repeat until loops. Use a for loop or a while loop instead. No one likes reapeat until loops.

0

I'm not sure if the "repeat until" is the source of my problem but, I rewrote the script and it works now (you can look at to my answer if you wanna learn the script that works)

1

Repeat loops are your own choice, it works, but you need to define a variable. For loops does not need a variable and it returns the same thing "No one likes repeat until loops" is not correct since repeat loops are way easier for beginners

0

I agree. Both of these loops have pretty similar logic and they're almost the same, but as you said unlike "while" loops you should define a variable to make it work.

1

Repeats loop and while loops are the same thing, but I'd recommend repeat loop since it's more consistent and make your script looks better lol

2 answers

✓ Accepted answer

I would suggest you use for loop on this situation, since it only loops for a specific value and returns index value too. And it's more useful since it might fix your problem!

for x = 8.5, 0, 0.1 do
	wait(0.2)
	script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(x,0,0)
end

I've deleted the line of disabling own script. Since you cannot enable it back because you disabled the script and it stopped to run. Just leave it like that since it's the end, it won't execute anything!

2 comments

1

Thank you for the help!

0

No problem!!

Ok guys so, I fixed the problem myself.

First off, I used "while wait() do" instead (idk if the "repeat until" was the main problem).

Second, I changed;

until x == 0
script.Disabled=true

to this:

if x < 0.2 then
	script.Disabled=true
	break
end

because the X number never reaches to 0. So I used "less-than 0.3" instead of "equals to 0". It works pretty well now.

Here's the whole script:

local x = 8.5
while wait() do
	script.Parent.CFrame = script.Parent.CFrame * CFrame.Angles(x,0,0)
	script.Parent.Parent.EngineWheel2.CFrame = script.Parent.Parent.EngineWheel2.CFrame * CFrame.Angles(x,0,0)
	x -= 0.05
	if x < 0.2 then
	script.Disabled=true
	break
	end
end

3 comments

1

This is a not bad idea! Repeat loops will definitely work too. Good job for solving your own problem!

0

Ummm... You disabled your own script before running the break, it will not break since it's disabled heehee......

0

Haha lol. The reason why I put "break" after disabling the script because disabling it doesn't work, sometimes. (It keeps running the loop after it's disabled, idk why) Just in case lol. (I'll remove it next time, thx for informating me about it!) 

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