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

roblox simple transparency animation?

Asked by 5 years ago
Edited 5 years ago

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

2 answers

Log in to vote
0
Answered by 5 years ago

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.

1for i=1, 10 do
2    print("This code was repeated " .. i .. " times!")
3end

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

01local Part = script.Parent
02local go = true -- I didn't see this used anywhere, but I've kept it for consistency
03 
04for 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
View all 23 lines...

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...

1while 1 + 1 = 2 do
2    print("The loop ran!")
3end
1while true do
2    print("The loop ran!")
3end

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!

0
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 thebananashetoldyou 31 — 5y
0
I can feel my brain getting larger now when i'm processing the information im going to practice this to remember it thanks thebananashetoldyou 31 — 5y
Ad
Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
5 years ago

First of all, you would right the text you put in a script.

Use this code inside the brick:

1local this = script.Parent --Get the part
2this.Transparency = 1 --Make sure it's visible
3 
4for 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
7end

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:

1local this = script.Parent
2 
3while true do
4    if this.Transparency == 0 then
5        this.CanCollide = false
6    else
7        this.CanCollide = true
8    end
9end

Answer this question