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

can't make the transparency looping?

Asked by
Ali_MTR 30
4 years ago

what im trying to do with my script is that i want my brick's transparency to loop from 0 to 1 and goes back to 0 nonstop

i've tried several ways to code this but because i'm a beginner (started coding 1 week ago) i can't do it well

here is the latest one i tried

part = script.Parent
i = 0
while true do
    i = i + 0.1 
    part.Transparency = i
wait(0.1)
    if part.Transparency == 1 then
    i = i + -0.1
    part.Transparency = i
wait(0.1)
    if part.Transparency == 0 then
    i = i + 0.1 
    part.Transparency = i
wait(0.1)
    end
end
end


1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

here's how you would do that. I explain each step in the code:

part = script.Parent --location of your part in the workspace

while true do --this while true do loop makes the code inside the loop run indefinitely 


    --this loop starts at zero and goes up to one. every time the loop runs, 0.1 is being added to the variable i and we are changing part's transparency to i. The increments would go as following: (0, 0.1, 0.2 (...) 0.9 1 

    for i = 0, 1, .1 do
        part.Transparency = i
        wait(.1)
    end --end of the first for loop

    --This does the same as the other loop but instead of counting from 0 to 1 it counts down from 1 to zero. 
    for i = 1, 0, -.1 do
        part.Transparency = i
        wait(.1)
    end --end of the second for loop


end --end of the while loop

without all the comments it looks like this:

part = script.Parent

while true do
    for i = 0, 1, .1 do
        part.Transparency = i
        wait(.1)
    end


    for i = 1, 0, -.1 do
        part.Transparency = i
        wait(.1)
    end
end

you initially had it so you added 0.1 every .1 seconds, checked if it was 1, started subtracting. your issue was that once you subtract one, and then you're constantly adding one then subtracting one. if you wanted to do it your way, then you could've done this:

part = script.Parent
i = 0
incrementValue = -1

while wait(0.1) do
    --condition for when i is 1 or more
    if part.Transparency >= 1 then
        incrementValue = -0.1  --make i -0.1 so we're subtracting instead of adding
    end

    --condition for when i is less than or equal to zero
    if part.Transparency <= 0 then
        incrementValue = 0.1 --make i 0.1 so we're adding .1 instead of subtracting 
    end

    --update i and part make part transparency i
    i = i + incrementValue
    part.Transparency = i
end
0
omg im so dumb i did try "for" but i stack them all together, i'm so happy now thanks alot Ali_MTR 30 — 4y
0
Nah, you're not. You had the right idea, just the logic was a bit flawed. royaltoe 5144 — 4y
0
I updated the post to show what you could have done to make your way of doing it work royaltoe 5144 — 4y
Ad

Answer this question