hello i'm new to roblox scripting and I i'm trying to make a obby.I wanted to use "for loops" to make it so that the part becomes transparent and then after Transparency == 1 then it will go negative but i'm not sure whats wrong,why is it not looping? it does the code then it just stops happening
Part = script.Parent go = true
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
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.
1 | for i = 1 , 10 do |
2 | print ( "This code was repeated " .. i .. " times!" ) |
3 | 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
01 | local Part = script.Parent |
02 | local go = true -- I didn't see this used anywhere, but I've kept it for consistency |
03 |
04 | for i = 1 , 10 do -- This loop controls how many times the code is run in total. |
05 |
06 | for i = 0 , 1.05 , 0.05 do -- This loop makes the part invisible |
07 | Part.Transparency = i |
08 | wait( 0.1 ) |
09 | if Part.Transparency = = 1 then |
10 | wait( 5 ) |
11 | end |
12 | end |
13 |
14 | for i = 1.2 ,- 0.05 ,- 0.05 do -- This loop makes the part visible again |
15 | Part.Transparency = i |
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...
1 | while 1 + 1 = 2 do |
2 | print ( "The loop ran!" ) |
3 | end |
1 | while true do |
2 | print ( "The loop ran!" ) |
3 | 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!
First of all, you would right the text you put in a script.
Use this code inside the brick:
1 | local this = script.Parent --Get the part |
2 | this.Transparency = 1 --Make sure it's visible |
3 |
4 | for i = 1 , 100 , 1 do |
5 | this.Transparency = this.Transparency - 0.01 --Make the part a bit more transparent |
6 | wait( 0.01 ) --Wait a certain amount of time |
7 | end |
You can change the this.Transparency - 0.01 to any number, and change the wait time as well. This will change the speed the brick disappears. Also, if you want the players to phase through the brick, add this code in a separate script:
1 | local this = script.Parent |
2 |
3 | while true do |
4 | if this.Transparency = = 0 then |
5 | this.CanCollide = false |
6 | else |
7 | this.CanCollide = true |
8 | end |
9 | end |