Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't this transparency if then statement work?

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

✓ Accepted answer

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

3 comments

0

Why not just have the Transparency part in the while loop's arguments, and put the wait on this inside? ;P

0

This is shorter

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.