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

How to I make this script smaller?

Okay so the following script is involving a Gui. How would I shorten this script and make it do the same thing?

clicked = script.Parent.Parent.OverLay

	clicked.BackgroundTransparency = 0.9
	wait(0.1)
	clicked.BackgroundTransparency = 0.8
	wait(0.1)
	clicked.BackgroundTransparency = 0.7
	wait(0.1)
	clicked.BackgroundTransparency = 0.6
	wait(0.1)
	clicked.BackgroundTransparency = 0.5
	wait(0.1)
	clicked.BackgroundTransparency = 0.4
	wait(0.1)
	clicked.BackgroundTransparency = 0.3
	wait(0.1)
	clicked.BackgroundTransparency = 0.2
	wait(0.1)
	clicked.BackgroundTransparency = 0.1
	wait(0.1)
	clicked.BackgroundTransparency = 0

When I try to shorten it I come up with this result but it never works.

clicked = script.Parent.Parent.OverLay

for i = 1,10 do
wait(0.1)
clicked.BackgroundTransparency = clicked.BackgroundTransparency-0.1
end

Help please.

4 answers

✓ Accepted answer

I didn't test this personally!

for i = 1.0, 0, -0.1 do
	clicked.BackgroundTransparency = i
	wait(0.1)
end

Hope I helped

1 comment

0

Thank you :)

NOTE: The code used here is untested, but should work.

This is in fact easy to do, use a for loop like so:

clicked = script.Parent.Parent.OverLay

for i = 1, 10 do
clicked.BackgroundTransparency = clicked.BackgroundTransparency-0.1
wait(0.1)
end

This should loop ten times and each loop should bring the transparency down by 0.1.

Explanation

Ok, so a for loop is a very basic loop to use and takes three arguments: iterator variable (= start value), end value and increment. Ok so a for loop will start on the iterator value and will keep looping the given code until the iterator value (Which is added by the increment every time it loops) equals the end value.

The iterator value is written as a variable (For example; i = 1) and every time the for loop loops, the iterator value will be added with the increment until the start value (Which is the number the iterator value is assigned to) is equal to the end value.

So in this code the loop will loop ten times since it takes ten loops to make the start value equal to the end value. The increment is not written because it's actually an optional argument and if it's not defined (Such as in the answer code I gave you) the increment will be (By default) 1.

And yes, the iterator value can be used anywhere in the loop since it's a variable; for example:

for i = 1, 10 do
print('The iterator value is ' .. i)
wait()
end

For the official wiki page on the for loop (And the other loops), click here!

Hoped this helped

~coo1o

You would have to use a for loop, which runs a certain code an amount of times, in this case, 0.9 counting down to 0 which would be .9 times, so it would be run .9 times, an example of a for loop is..

for i = 0, 1, .1 do --Addresses that it starts from 0 and ends at 1, going up by .1's
	game.Workspace.Baseplate.Transparency = i --Labels the item
	wait() --Just so your studio won't crash when Playing Solo or Running
end --Ends the loop

If you are thinking of a while loop, well there is a difference. A while loop runs something an infinite amount of times, but can be broken with "break" An example of a while loop is..

while true do --Tells the kind of loop
	game.Workspace.Baseplate.CanCollide = false --Labels the functions
	wait(1)
	game.Workspace.Baseplate.CanCollide = true
end

Or you can substitute the "true" part in "while true do" with things such was wait() or 5 < 9 or 5 > 9 which of course would run if that equation is true, so answering your question, you would do a for loop:

clicked = script.Parent.Parent.OverLay

for i = .9, 0, -.1 do --Labels the type of loop, starts at .9, ends at 0, goes down by 1
	clicked.BackgroundTransparency = i --Labels what its used on
	wait() --Incase of studio crash
end --Ends

Hope it helped :)

for i = minimum,maximum do
minimum = 1
maximum = 10
wait(0.1)
Clicked.Background.Transparency = Clicked.Background.Transparency-0.1

If that doesn't work, just do yours but try

clicked.Background.Transparency

Instead of the current way you were doing it. You're welcome if it works though, I'm not the brightest of all scripters, I tried my best.

1 comment

1
  1. You defined minimum and maximum inside the for loop 2) You forgot an end 3) It's BackgroudTransparency

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