what im trying to do with my script is that i want my brick's transparency to loop from 0 to 1 and goes back to 0 nonstop
i've tried several ways to code this but because i'm a beginner (started coding 1 week ago) i can't do it well
here is the latest one i tried
part = script.Parent i = 0 while true do i = i + 0.1 part.Transparency = i wait(0.1) if part.Transparency == 1 then i = i + -0.1 part.Transparency = i wait(0.1) if part.Transparency == 0 then i = i + 0.1 part.Transparency = i wait(0.1) end end end
here's how you would do that. I explain each step in the code:
part = script.Parent --location of your part in the workspace while true do --this while true do loop makes the code inside the loop run indefinitely --this loop starts at zero and goes up to one. every time the loop runs, 0.1 is being added to the variable i and we are changing part's transparency to i. The increments would go as following: (0, 0.1, 0.2 (...) 0.9 1 for i = 0, 1, .1 do part.Transparency = i wait(.1) end --end of the first for loop --This does the same as the other loop but instead of counting from 0 to 1 it counts down from 1 to zero. for i = 1, 0, -.1 do part.Transparency = i wait(.1) end --end of the second for loop end --end of the while loop
without all the comments it looks like this:
part = script.Parent while true do for i = 0, 1, .1 do part.Transparency = i wait(.1) end for i = 1, 0, -.1 do part.Transparency = i wait(.1) end end
you initially had it so you added 0.1 every .1 seconds, checked if it was 1, started subtracting. your issue was that once you subtract one, and then you're constantly adding one then subtracting one. if you wanted to do it your way, then you could've done this:
part = script.Parent i = 0 incrementValue = -1 while wait(0.1) do --condition for when i is 1 or more if part.Transparency >= 1 then incrementValue = -0.1 --make i -0.1 so we're subtracting instead of adding end --condition for when i is less than or equal to zero if part.Transparency <= 0 then incrementValue = 0.1 --make i 0.1 so we're adding .1 instead of subtracting end --update i and part make part transparency i i = i + incrementValue part.Transparency = i end