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

Is there a way to fix the problem of the script subtracting 1 when the IntValue hits to 0?

Asked by
TheePBHST 154
8 years ago
Edited 8 years ago

Hello to the person reading this. My script is refusing to stop subtracting a number when it hits 0.

Here is the script

01local Inter = Instance.new("IntValue") -- Creates the Value
02Inter.Parent = game.Workspace
03Inter.Name = ("Intermission")
04Inter.Value = 10
05 
06while true do
07    repeat
08        wait(1)
09        Inter.Value = Inter.Value -1
10 
11    until Inter.Value == 0
12end

4 answers

Log in to vote
1
Answered by
Uroxus 350 Moderation Voter
8 years ago

Un-necessary to have a repeat within a while loop. You can simply edit your while loop by replacing the true with the conditional...

1local Inter = Instance.new("IntValue") -- Creates the Value
2Inter.Parent = game.Workspace
3Inter.Name = ("Intermission")
4Inter.Value = 10
5 
6while Inter.Value > 0 do
7    wait(1)
8    Inter.Value = Inter.Value - 1
9end
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

@Uroxus's answer is the correct way to do it.

Below works but isn't correct.

01local Inter = Instance.new("NumberValue") -- Creates the Value
02Inter.Parent = game.Workspace
03Inter.Name = ("Intermission")
04Inter.Value = 10
05 
06repeat
07    if Inter.Value ~= 0 then
08        wait(1)
09        Inter.Value = Inter.Value -1
10    end
11until Inter.Value == 0
Log in to vote
0
Answered by 8 years ago

A repeat loop checks its condition last, meaning it will always run the code inside at least once. I'm not sure what you're trying to do, but this would fix that problem:

01local Inter = Instance.new("IntValue") -- Creates the Value
02Inter.Parent = game.Workspace
03Inter.Name = ("Intermission")
04Inter.Value = 10
05 
06while true do
07    while Inter.Value ~= 0 do
08        wait(1)
09        Inter.Value = Inter.Value -1
10    end
11    wait()
12end

However this is bad code. Instead a simple for loop would do:

1local Inter = Instance.new("IntValue") -- Creates the Value
2Inter.Parent = game.Workspace
3Inter.Name = ("Intermission")
4Inter.Value = 10
5 
6for i = Inter.Value, 0, -1 do
7    wait(1)
8    Inter.Value = i
9end

If this isn't what you wanted please explain in greater detail what you want the code to do.

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago
01local Inter = Instance.new("IntValue") -- Creates the Value
02 
03Inter.Parent = game.Workspace
04 
05Inter.Name = ("Intermission")
06 
07Inter.Value = 10
08 
09    repeat
10        wait(1)
11        Inter.Value = Inter.Value -1
12    until Inter.Value == 0

Its an infinite loop. The while true do part kept repeating the repeat wait(1) until part.

Answer this question