Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I not compare a "number with a nil?"

function assignScientists(amount)
   local valid_players = {}
   local player = game.Players:GetChildren()
   if(#player>0)then
   for p = 1, #player do
   if(player[p].TeamColor==Americans_team.TeamColor)and
   (player[p].Character~=nil)then
   table.insert(valid_players,player[p])
   end
   end
   end
   if(#valid_players>amount)then
   while(amount>0)do
   local rand = math.random(1,#valid_players)
   local chosen = valid_players[rand]
   chosen.TeamColor = Europeans_team.TeamColor
   chosen.Character.Humanoid.Health = 0

   table.remove(valid_players,rand)
   amount = amount -1
   end
   end
 end
 assignScientists()

In the output value, it kept saying "attempt to compare number with a nil" at line 13. I put a 0 there but yet it still says the same thing. Can anyone help me? Thank you for reading.

6 comments

0

If there are no children within "Players" at all, then it will add nothing. Which could possibly make player = nil

0

Where is it saying this at? Also nil means nothing so that's why it isn't a zero.

0

Are you getting this error when you "Play" in studio? Does it work in solo and or server mode?

0

It says at Line 13 and I was on server mode.

1

Oh, I think the parameter is wrong.

0

amount doesn't know where to start. Define it at the bottom

2 answers

function assignScientists(amount) -- This function is looking for a parameter
   local valid_players = {}
   local player = game.Players:GetChildren()
   if(#player>0)then
   for p = 1, #player do
   if(player[p].TeamColor==Americans_team.TeamColor)and
   (player[p].Character~=nil)then
   table.insert(valid_players,player[p])
   end
   end
   end
   if(#valid_players>amount)then
   while(amount>0)do
   local rand = math.random(1,#valid_players)
   local chosen = valid_players[rand]
   chosen.TeamColor = Europeans_team.TeamColor
   chosen.Character.Humanoid.Health = 0

   table.remove(valid_players,rand)
   amount = amount -1
   end
   end
 end
 assignScientists() --You have no parameters

Try adding a parameter to assignScientists() at the bottom. This may fix the issue with amount being nil.

Example: assignScientists(0)

1 comment

0

Thats right

 function playScientists()
     if countPlayers()<=3 then
     assignScientists(1)

Well, I added another code that was like this but it didn't work.

6 comments

0

Are they within the same script?

0

Yes. They are within the same script.

0

Same error? (Also, instead of posting an answer, just edit your original post. Prevent confusion)

0

Okay, I will post it in the same thing. Yes, it still has the same error. If you want the whole script, I can message you it.

0

If you message me, I may not get to it until later in the day. I am currently at work. (It's quiet)

0

Okay.

This discussion is preserved as it was. New answers, comments, and votes are closed.