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

For loop not functioning as intended, why?

Asked by 4 years ago
Edited 4 years ago

Please dont get mad at me because of my horrible scripting. I am new and am trying to learn, anyways, I tried this script and it doesnt seem to work, can someone tell me what I did wrong and how to fix it (ofc if your allowed). I can respond tomorrow morning if anyone needs clearification. Thank you for your time! (: New to scripting, again pls dont get mad <: I wanted to make a loop that changes a parts transparency back and forth 200 times.

for i = 1, 200 do
    while true do
        game.Workspace.Part.Transparency = 0.5
        wait(2)
        game.Workspace.Part.Transparency = 1
    end
end

2 answers

Log in to vote
0
Answered by 4 years ago

So do you know what does the function "while true do"? Because what it does is to repeat the code inside of it infinitely. The problem is that you put a while true do in a for loop so that when you run the code, the line of code 3,4 and 5 will run infinitely. What I suggest is to delete that while true do:

for i = 1, 200 do
game.Workspace.Part.Transparency = 0.5
wait(2)
game.Workspace.Part.Transparency = 1
end
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You could also use this method as stated above, except this loop stops once it hits the specified transparency you supply in the end_transparency

start_transparency = 0.5 -- 0.5 is the starting transparency
end_transparency = 1.5 --1.5 is the ending transparency

for transparency = start_transparency, end_transparency do wait()
    workspace.Part.Transparency = transparency
end

-- to reverse it just swap the start and end around such as 

for transparency = end_transparency, start_transparency do wait()
    workspace.Part.Transparency = transparency
end

Answer this question