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

for loop is not gradually decreasing in number?

Asked by 7 years ago

Hi all,

I have a local script which sets the bloom value to 5 then it should gradually decrease back to 0. The problem is, it decreases to 0 very quickly and when I try to reduce the speed of it, the script breaks:

game.ReplicatedStorage:WaitForChild("FlashEvent").OnClientEvent:connect(function()
    print ("worked")
local bloom = Instance.new("BloomEffect",nil)
bloom.Intensity = 5
bloom.Threshold = 0
bloom.Parent = game.Lighting
for i = 1,bloom.Intensity do
bloom.Intensity = bloom.Intensity-1
wait(1/10)
end
end)


Instead of reducing bloom.Intensity by 1 each time I want to decrease it by .1

But when I tried that, the bloom.Intentsity just gets stuck at 4.5.

I also want this effect to last for a total of 5 seconds, how can I fix this script to do it?

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

So the problem is within your for loop here,

for i = 1,bloom.Intensity do
    bloom.Intensity = bloom.Intensity-1
    wait(1/10)
end

We can decrease a for loop easily by adding another number which is how much it iterates by.

Here are some examples of what I mean.

for i = 1,10 do
    print(i)
end

-- Is the same as
for i = 1,10,1 do
    print(i)
end

--// OUTPUT
1
2
3
4
5
6
7
8
9
10
Counting Up ^
for i = 10,1,-1 do -- You must place the negative 1, or else it will default to positive 1.
    print(i)
end
Counting Down ^

We can also use decimals.

--// Method one
for i = 5,0,-0.1 do
    print(i)
end

-- Is roughly the same as,
--// Method two
for i = 50,0,-1 do
    print(i/10)
end

--// METHOD ONE OUTPUT
5.0
4.9
4.8
4.7
4.6
4.5
4.4
4.3
4.2
4.1
4.0
3.9
3.8
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0
2.9
2.8
2.7
2.6
2.5
2.4
2.3
2.2
2.1
2.0
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1.0269562977783e-15

--// METHOD TWO OUTPUT,
5.0
4.9
4.8
4.7
4.6
4.5
4.4
4.3
4.2
4.1
4.0
3.9
3.8
3.7
3.6
3.5
3.4
3.3
3.2
3.1
3.0
2.9
2.8
2.7
2.6
2.5
2.4
2.3
2.2
2.1
2.0
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0.0

Notice the second method ends with a zero, and not a floating point error decimal. This is why the second method is considered cleaner and better.

Applying this to your code, we get something like this,

for i = bloom.Intensity*10,0,-1 do
    bloom.Intensity = i/10
    wait()
end

If you have any further messages or concerns, let me know.

1
Thank you so much for this answer! There were a few other scripts I also created to gradually increase/ decrease a value. But they all had float points. With this, the issue is now rectified! Alectfenrir123 10 — 7y
0
I'm glad I could help c: OldPalHappy 1477 — 7y
Ad

Answer this question