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

Why won't this transparency if then statement work?

Asked by
Nik1080 24
7 years ago
Edited 7 years ago

Basically, I was toying with some transparency scripts and wanted to try doing a transparency fading script. This was my attempt:

if script.Parent.Transparency < 1 then
        script.Parent.Transparency = script.Parent.Transparency + 0.1
        else
    script.Parent.Transparency = 0
end

For whatever reason, it's not working. I'm not getting errors or anything in the output. Can anybody explain or correct my script?

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago

Problem

You're not looping through the if statement. Therefore, it will only run once on execution.


Solution

Put the code in a while loop. In fact, you could just place the logic in the while loop as well. while (condition(s) (is/are) true) do. This way you don't even need the while loop.


Final Script

while wait() and script.Parent.Transparency < 1 do --While waiting 1/30 of a second on top of the Transparency still being less than 1, continue with the loop.
    script.Parent.Transparency = script.Parent.Transparency + .1 --Add .1 to Transparency and set that as the new transparency.
end

As a reminder, Gui elements should be handled by LocalScripts.


Hopefully this answered your question. If it did, hit the accept answer button. If you have any questions, feel free to leave them in the comments below.
0
Why not just have the Transparency part in the while loop's arguments, and put the wait on this inside? ;P TheeDeathCaster 2368 — 7y
0
This is shorter Nik1080 24 — 7y
0
Because wait will always be true, it's just the matter of Transparency being true with the "and" operator. Now, if you had a thousand conditions, then I would recommend breaking it up. However in this scenario, it works. M39a9am3R 3210 — 7y
Ad

Answer this question