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 4 years ago
Edited 4 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 4 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.

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 -- I didn't see this used anywhere, but I've kept it for consistency

for i=1, 10 do -- This loop controls how many times the code is run in total.

    for i = 0,1.05,0.05 do -- This loop makes the part invisible
        Part.Transparency = i
        wait(0.1)
        if Part.Transparency == 1 then
            wait(5)
        end
    end

    for i = 1.2,-0.05,-0.05 do -- This loop makes the part visible again
        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!

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 — 4y
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 — 4y
Ad
Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
4 years ago

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

Use this code inside the brick:

local this = script.Parent --Get the part
this.Transparency = 1 --Make sure it's visible

for i = 1,100,1 do
    this.Transparency = this.Transparency - 0.01 --Make the part a bit more transparent
    wait(0.01) --Wait a certain amount of time
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:

local this = script.Parent

while true do
    if this.Transparency == 0 then
        this.CanCollide = false
    else
        this.CanCollide = true
    end
end

Answer this question