Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 2 years ago
Edited 2 years ago

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?

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. Spjureeedd 385 — 2y
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) Va1t_Dev 86 — 2y
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 Xapelize 2658 — 2y
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. Va1t_Dev 86 — 2y
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 Xapelize 2658 — 2y

2 answers

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago

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!

1
Thank you for the help! Va1t_Dev 86 — 2y
0
No problem!! Xapelize 2658 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

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
1
This is a not bad idea! Repeat loops will definitely work too. Good job for solving your own problem! Xapelize 2658 — 2y
0
Ummm... You disabled your own script before running the break, it will not break since it's disabled heehee...... Xapelize 2658 — 2y
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!)  Va1t_Dev 86 — 2y

Answer this question