nezzled
nezzled

Reputation: 93

Roblox Lua if statement still executing even when the value isn't true

this has been a major problem in my game. I have been trying to make a script that will detect when a value is 1, 2, 3, 4, or 5. However, the if statements will still execute even if the value is 0, making the timer start before anyone can get into the elevator. This is really annoying, and I cannot fix it. Here is the script:

local players = workspace.TestMode.Players
players.Value = 0
wait(5)
script.Parent.Text = "Waiting for players..."

function StartTimer()
  while true do
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      script.Parent.Text = "15"
      print("enough players")
      wait(0.1)
    else
      script.Parent.Text = "Waiting for players..."
      print("not enough players")
    end 
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "14"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "13"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "12"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "11"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "10"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "9"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "8"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "7"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "6"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "5"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "4"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "3"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "2"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "1"
    else
      script.Parent.Text = "Waiting for players..."
    end
    wait(0.1)
    if players.Value == 1 or 2 or 3 or 4 or 5 then
      wait(0.9)
      script.Parent.Text = "Teleporting players..."
    else
      script.Parent.Text = "Waiting for players..."
    end
  end
end
StartTimer()

Upvotes: 3

Views: 493

Answers (2)

PikachuPlays
PikachuPlays

Reputation: 49

This is not an answer because the person above answered but this is just to tell you what was wrong with your script so you can learn from it. You did if players.Value == 1 or 2 or 3 or 4 or 5. When you use an if statment and then you have an or statement inside of it. You have to completely restart the code. For example it would have worked if it was if players.Value == 1 or players.Value == 2 or players.Value == 3, etc. That is why it wasn't working.

Upvotes: 0

Nifim
Nifim

Reputation: 5031

In Lua all values other then nil and false are truthy so when evaluated in a boolean context they will be treated as true.

if players.Value == 1 or 2 or 3 or 4 or 5 then

This means that your if condition is players.Value == 1 true or is 2 true and so on. 2 is always true. so your if condition is always true.

your condition should look like:

if players.Value == 1 or players.Value == 2 or players.Value == 3 or players.Value == 4 or players.Value == 5 then

alternatively you could have a simpler condition here are some ideas:

if players.Value > 0 and players.Value <= 5 then
conditions = {[1] = true, [2] = true, [3] = true, [4] = true, [5] = true}

if conditions[players.Value] then

Upvotes: 5

Related Questions