Loops
HOLD UP! Before we get into repeating your code, it's probably safe to assume that your code is WAY to long for what it could actually be.
To give a quick example of how we can change your code, let's first do something else.
Let's make a script that prints every number from 1 to 10:
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)
Now, the above code does indeed get the job done.
However, we can make the code a LOT shorter.
Take a look at this:
for i = 1, 10 do
print(i)
end
What was done above is called a loop. It allows you to go through a series of value(s) very easily. Basically, it sets the variable 'i' to 1. Each time the chunk of code between the 'for' and the 'end' has finished running, 'i' will increase by '1'. It does this till it reaches 11. That's why the code will print every number from 1 - 10 inclusive.
We can even change the increment by adding a third number!
for i = 1, 10,2 do
print(i)
end
The third number will print the code at counts of '2'.
So it will print, 1, 3, 5, 7, 9, 10
Using What We've Learnt
Now, let's make your code shorter AND repeat.
for transparency = .1,1,.1 do
workspace.Base.Transparency = transparency
wait(.5)
end
workspace.Base.CanCollide = false
wait(.5)
workspace.Base.CanCollide = true
for transparency = 1,.1,-.1 do
workspace.Base.Transparency = transparency
wait(.5)
end
Great! We've shortened your code by using a loop, but let's make it repeat forever. We can use a while loop to achieve this.
while true do
for transparency = .1,1,.1 do
workspace.Base.Transparency = transparency
wait(.5)
end
workspace.Base.CanCollide = false
wait(.5)
workspace.Base.CanCollide = true
for transparency = 1,.1,-.1 do
workspace.Base.Transparency = transparency
wait(.5)
end
end
Basically, a while loop will keep repeating if whatever you put between the 'while' and 'do' is true. So if we put 'true', then it will repeat forever since true is always true. [Sorry if that sounds weird]
For more on loops
There are a lot of sources that teach loops. Loops are a highly essential part to certain aspects of coding.
Helpful Links
http://wiki.roblox.com/index.php?title=Basic_Scripting
http://wiki.roblox.com/index.php?title=Loops
http://wiki.roblox.com/index.php?title=Absolute_beginner%27s_guide_to_scripting
3 comments
Error in line 2
It is worth mentioning that they are 'told to break' when the condition becomes false or until someone puts 'break'
Ah, thank you.