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
7 years ago
Edited 7 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

local Inter = Instance.new("IntValue") -- Creates the Value
Inter.Parent = game.Workspace
Inter.Name = ("Intermission")
Inter.Value = 10

while true do
    repeat
        wait(1)
        Inter.Value = Inter.Value -1

    until Inter.Value == 0
end

4 answers

Log in to vote
1
Answered by
Uroxus 350 Moderation Voter
7 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...

local Inter = Instance.new("IntValue") -- Creates the Value
Inter.Parent = game.Workspace
Inter.Name = ("Intermission")
Inter.Value = 10

while Inter.Value > 0 do
    wait(1)
    Inter.Value = Inter.Value - 1
end

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

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

Below works but isn't correct.

local Inter = Instance.new("NumberValue") -- Creates the Value
Inter.Parent = game.Workspace
Inter.Name = ("Intermission")
Inter.Value = 10

repeat
    if Inter.Value ~= 0 then
        wait(1)
        Inter.Value = Inter.Value -1
    end
until Inter.Value == 0

Log in to vote
0
Answered by 7 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:

local Inter = Instance.new("IntValue") -- Creates the Value
Inter.Parent = game.Workspace
Inter.Name = ("Intermission")
Inter.Value = 10

while true do
    while Inter.Value ~= 0 do
        wait(1)
        Inter.Value = Inter.Value -1
    end
    wait()
end

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

local Inter = Instance.new("IntValue") -- Creates the Value
Inter.Parent = game.Workspace
Inter.Name = ("Intermission")
Inter.Value = 10

for i = Inter.Value, 0, -1 do
    wait(1)
    Inter.Value = i
end

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 7 years ago
Edited 7 years ago
local Inter = Instance.new("IntValue") -- Creates the Value

Inter.Parent = game.Workspace

Inter.Name = ("Intermission")

Inter.Value = 10

    repeat
        wait(1)
        Inter.Value = Inter.Value -1
    until Inter.Value == 0

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

Answer this question