Your code isn't looping because you haven't told it to. Depending on how many times you wish to loop, there are different functions supported by Lua to achieve this.
For Loops
Let's say you wanted to loop the code 10 times. For this, you would have to wrap it within a for loop to instruct the script to repeat this code over 10 increments.
Here is an example of a for loop in its most basic form. You can see that i is set to 1 as the start integer, and the end integer is set to 10.
for i=1, 10 do
print("This code was repeated " .. i .. " times!")
end
For what you're trying to achieve, all you'd need to do is add the loops already written inside another loop!
See the example code below
local Part = script.Parent
local go = true
for i=1, 10 do
for i = 0,1.05,0.05 do
Part.Transparency = i
wait(0.1)
if Part.Transparency == 1 then
wait(5)
end
end
for i = 1.2,-0.05,-0.05 do
Part.Transparency = i
wait(0.1)
if Part.Transparency == 0 then
wait(4)
end
end
print("Script has looped " .. i .. " times!")
end
But let's say you wanted to run this code an unlimited number of times (or until a condition is met), for that we can use...
While Loops
Much like how we've just demonstrated how to use for loops with your code, all you'd need to do is add the code inside a while loop.
A while loop is a loop that will run until a condition is met. This could be a variable, or for a never-ending loop, something such as 1 + 1 = 2 would suffice as it always results as true. On that note, simply using true as your condition would also work.
Here's a few examples using what we just talked about...
while 1 + 1 = 2 do
print("The loop ran!")
end
while true do
print("The loop ran!")
end
For your code, you could use either of these, or any of the many loop varieties available on the Developer Hub (https://developer.roblox.com/en-us/articles/Loops).
Hope this helps!
2 comments
oh thanks i understand now the difference between for loops and while loops so if i wanted my code to loop only like 5 times than i would use for loops but if i wanted to loop it forever i will use a while loop this also helped fix my problem thanks
I can feel my brain getting larger now when i'm processing the information im going to practice this to remember it thanks