Problems with "for"

Hello users, today i have a problem about scripts with “for”.
There is the scripts:

for v = 10,0,-1 do
if game.ReplicatedStorage.InGame.Value == false then
script.Parent.Text = "left "…v
if script.Parent.Text == “left 0” then
print(“good”)
game.ReplicatedStorage.InGame.Value = true
elseif game.ReplicatedStorage.InGame.Value == true then
print(“true”)
game.ReplicatedStorage.InGame.Value = false
end
end
end

The Bool Value (InGame) is set to “false” by default, but when InGame is set to “true” elseif doesn’t work. Any way to resolve this?

“There is the scripts” = “Here is the script”, sorry

Instead of doing:

for i = 10, 0, -1

Do:

for i = 1, 10

this will make the script go from 1 to 10 and stand still at 10 which makes it impossible to reach 0.

Did you put any waits inside the script?

The reasoning is because the elseif statement is tied to the

statement rather than the

statement.

Reposition your ends to fix this.

Correct code:

for v = 10,0,-1 do
if game.ReplicatedStorage.InGame.Value == false then
script.Parent.Text = "left "…v
if script.Parent.Text == “left 0” then
print(“good”)
game.ReplicatedStorage.InGame.Value = true
end
elseif game.ReplicatedStorage.InGame.Value == true then
print(“true”)
game.ReplicatedStorage.InGame.Value = false
end
end

(Ignore the poor indentation, on mobile)

If you want it to reach 0, then use this:

for i = 0, 10, -1 do

You used:

for i = 10, 0, -1

That was what I had already put.

Please read my reply carefully, it’s different than what you put.

In addition, I advise you to change this line:

if script.Parent.Text == “left 0” then

To this:

if v <= 0 then

The script now doesn’t seem to work even though I don’t know why. did you test it?

I was looking at the suggestions they sent but the script now doesn’t even give messages if it is false or true.

That’s because elseif is in the wrong scope. Here’s the fixed code

for v = 10,0,-1 do
    if game.ReplicatedStorage.InGame.Value == false then
        script.Parent.Text = "left " .. v
        if script.Parent.Text == "left 0" then
            print("good")
            game.ReplicatedStorage.InGame.Value = true
        end
    elseif game.ReplicatedStorage.InGame.Value == true then
        print("true")
        game.ReplicatedStorage.InGame.Value = false
    end
end

the script still cannot detect if the other is true when activated at the top.

maybe it’s because it’s a local script?

Where do you change the value of InGame?

It is located in ReplicatedStorage, it is set to false by default, the script checks if it is false and returns to true but then the script does not seem to check whether it is true or not so that it is disabled again

I will have to leave I will see what you posted again, maybe the mistake is mine because the script is not well done.

That’s because in the last iteration of the loop, when v is equal to 0 the first condition is met, the value is set to true and the loop is finished, it doesn’t check the elseif condition because that’s how elseif works.

if condition1 then
-- 1
elseif condition2 then
-- 2
end

If condition1 is true, then it will not check condition2. You would have to do something like that:

for v = 10,0,-1 do
    if game.ReplicatedStorage.InGame.Value == false then
        script.Parent.Text = "left " .. v
        if script.Parent.Text == "left 0" then
            print("good")
            game.ReplicatedStorage.InGame.Value = true
        end
    end
    if game.ReplicatedStorage.InGame.Value == true then
        print("true")
        game.ReplicatedStorage.InGame.Value = false
    end
end

Also, if you are doing that in a local script, then the changes won’t replicate to the server, but this script will work despite that. The only problem with the local script is that you won’t see the changes on the server and on other clients.

1 Like

well it seems that after all I still don’t know everything about the elseif, thanks

1 Like