Repeating the change of a part's transparency

Hey All,

I’m still a beginner. The basic problem is trying to make a part loop through varying transparency values: 0, 0.1, 0.2…1, repeat over and over. I have a part in the workspace with a script inserted. The script below is what I’m using to change the transparency of the part. The loop isn’t repeating. In game play, the transparency only changes once from 0 to 0.1, then nothing. Can you point out my coding flaw for why this isn’t repeating? Thank you!

local glass = script.Parent

while true do
	if glass.Transparency == 1 then
		glass.Transparency = 0
	elseif glass.Transparency == 0.9 then
		glass.Transparency = 1
	elseif glass.Transparency == 0.8 then
		glass.Transparency = 0.9
	elseif glass.Transparency == 0.7 then
		glass.Transparency = 0.8
	elseif glass.Transparency == 0.6 then
		glass.Transparency = 0.7
	elseif glass.Transparency == 0.5 then
		glass.Transparency = 0.6
	elseif glass.Transparency == 0.4 then
		glass.Transparency = 0.5
	elseif glass.Transparency == 0.3 then
		glass.Transparency = 0.4
	elseif glass.Transparency == 0.2 then
		glass.Transparency = 0.3
	elseif glass.Transparency == 0.1 then
		glass.Transparency = 0.2
	elseif glass.Transparency == 0 then
		glass.Transparency = 0.1
	
	print("Looped worked")
	
end
wait(.1)

end

You should instead do this:

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

I am sure you’ve felt like writing that many if statements is impractical, ha.

To answer your question though, the loop is not repeating because after it runs through once, all of the if statements will be false. You could fix this by setting the transparency to 1 at the end of the loop again.

EDIT: Not to mention your wait is outside of the loop, which means it will time out. Make sure to put that inside the loop.

3 Likes

I think a better alternative in your case would be either a for loop (something like:

for i = 0, 1, 0.1 do
    glass.Transparency = i
    wait()
end

) or using TweenService.

3 Likes

And as for your coding flaw, you failed to add ends to your if/elseif statements- lua does not support these kinds of one-liners.

1 Like

Thanks for the input. Using the suggestion of the for loop made it work perfectly. So simple in the end. I did so much unnecessary work typing out all those lines.

1 Like

Let me take this to the next level. Let’s say I have 3 parts in a folder. I want to use 1 script to change the transparency of all 3 of those parts. I’ve written the script below, thinking it would affect all the parts, but it’s only changing the transparency of 1 of the parts. I’m assuming something is missing to allow all parts to be affected independently. Here is my attempted script. I could write a script for each part, but that would be excessive. Any thoughts?

local glassTable = script.Parent.GlassFolder:GetChildren()

for _, glass in pairs(glassTable) do
	
	while true do
		
		for i = 0,1,.1 do
		     glass.Transparency = i
		    wait(.1)
		end
	end
end

In some way i fixed your script i put while true do at the third line to see what happened and it worked but it goes like this way

local glassfolder = game.Workspace.GlassFolder:GetChildren()

while true do
for _,v in pairs(glassfolder) do
		for i = 0,1,0.1 do
		 v.Transparency = i
		 wait(0.1)
	end
end
end

1 Like

Nice coding there getting it to work. The inserted video is above and beyond excellent!

How did you get the video to show up as a video in your post? I tried to upload a video in another post and it just inserted a link.

This is going off topic but i just download a video and drag the file inside here