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

Im trying to decrease transparency with a for loop, but its not working. Tips?

i have a barrier, and I want to decrease the transparency by 0.1 until it is 0 so that its visible, by using a for loop. I also want it to wait 5 seconds before every 0.1 reduction. However it keeps going to the statement barrier 1 is completed, but its not even visible.

Barrier1 = game.Workspace.Barrier1

Barrier1.Transparency = 1
Barrier1.CanCollide = false
Barrier1.Anchored = true
cost = 50
script.Parent.MouseButton1Click:connect(function()
	
	if game.Players.LocalPlayer.leaderstats.Cash.Value >= cost then
		
		game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - cost
		
		Barrier1.Anchored = true
		local BarrierTransparency = game.Workspace.Barrier1.Transparency
		
		for BarrierTransparency=1, 0, -0.1  do
			if BarrierTransparency == 0 then
				Barrier1.CanCollide = true
				Barrier1.Transparency = 0
			end
			
		end
		
		
		print("Your barrier has been completed")
	else
		
		print("Not enough cash")
		
	
	end
end)

2 answers

Instead of using for, use repeat and until statements instead

Replace:

for BarrierTransparency=1, 0, -0.1  do
            if BarrierTransparency == 0 then
                Barrier1.CanCollide = true
                Barrier1.Transparency = 0
            end

        end

With:

repeat
Barrier1.Transparency = Barrier1.Transparency - 0.1 -- Will decrease transparency by "0.1"
wait(XX) --Replace with a time
until Barrier1.Transparency == 0
Barrier1.CanCollide = true
Barrier1.Transparency = 0

Try it out and let me know what happens

2 comments

0

Everything seemed to work fine excpet the Barrier1.CanCollide = true. It seems that it doesnt apply after the object isn't transparent.

0

@Awesomewebm I also put a print statement saying "Past wait time" after the wait, and in the output you can just see that that number next to the statement is increasing infinitly.

for loops already yield, so you don't need to check if the for loop has ended inside of it. also, that is not how for loops work. for loops generally iterate a certain amount of times, along with the iteration variable.

local Barrier1 = game.Workspace.Barrier1
for i =1, 0, -0.1  do
	Barrier1.Transparency = i
	wait()
end
Barrier1.CanCollide = true
print("Your barrier has been completed")

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