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

It keeps saying "Attempt to compare number with nil"? Can anyone help?

Asked by
Yeevivor4 155
9 years ago
function checkSurvivorNumber(count)
    local count = 0
    local players = game.Players:GetChildren()
    for i = 1, #players do
        if players[i] ~= nil then
        if players[i].TeamColor == BrickColor.new("Bright blue") then
                count = count + 1
                print("Checking Survivors")

            end
       end
    end
end

if count < 1 or count == nil then
     time.Value = 0 
     for i = 1,#players do
             if players[i].TeamColor == BrickColor.new("Bright red") then
         game.Workspace.DebugOutput.Value = "Awarding Killer's points"
               end
            end
       game.Workspace.Killerwin.Value = true
       end
       print("Checking if killers won")

On Line 15, it keeps saying "Attempt to compare number with nil". I've tried everything and I'm still confused. I'm new to scripting and I just wanted to know why it caused this. Even if I delete COUNT == NIL it still says the same thing over and over.

0
You should start by tabbing your code properly. BlueTaslem 18071 — 9y
0
How would I do that? Yeevivor4 155 — 9y
0
Count is a local variable. If you were to try to use it outside of that function, it would return as nil. Aethex 256 — 9y
0
So how shall I solve it, good sir? Yeevivor4 155 — 9y
View all comments (2 more)
0
Switch the comparisons. "if count == nil or count < 1 then" GoldenPhysics 474 — 9y
0
I think I solved it. I think I've added too many "ends" to my script . I feel much better now. Thank you for trying to help me. If you want me to give you a check mark, just post it. Yeevivor4 155 — 9y

1 answer

Log in to vote
-1
Answered by 9 years ago

Put local count outside the function and it will fix.

Ad

Answer this question