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

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

Asked by
Yeevivor4 155
10 years ago
01function assignScientists(amount)
02   local valid_players = {}
03   local player = game.Players:GetChildren()
04   if(#player>0)then
05   for p = 1, #player do
06   if(player[p].TeamColor==Americans_team.TeamColor)and
07   (player[p].Character~=nil)then
08   table.insert(valid_players,player[p])
09   end
10   end
11   end
12   if(#valid_players>amount)then
13   while(amount>0)do
14   local rand = math.random(1,#valid_players)
15   local chosen = valid_players[rand]
View all 24 lines...

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.

0
If there are no children within "Players" at all, then it will add nothing. Which could possibly make player = nil Necrorave 560 — 10y
0
Where is it saying this at? Also nil means nothing so that's why it isn't a zero. wjs3456 90 — 10y
0
Are you getting this error when you "Play" in studio? Does it work in solo and or server mode? Necrorave 560 — 10y
0
It says at Line 13 and I was on server mode. Yeevivor4 155 — 10y
View all comments (2 more)
1
Oh, I think the parameter is wrong. Yeevivor4 155 — 10y
0
amount doesn't know where to start. Define it at the bottom wjs3456 90 — 10y

2 answers

Log in to vote
4
Answered by
Necrorave 560 Moderation Voter
10 years ago
01function assignScientists(amount) -- This function is looking for a parameter
02   local valid_players = {}
03   local player = game.Players:GetChildren()
04   if(#player>0)then
05   for p = 1, #player do
06   if(player[p].TeamColor==Americans_team.TeamColor)and
07   (player[p].Character~=nil)then
08   table.insert(valid_players,player[p])
09   end
10   end
11   end
12   if(#valid_players>amount)then
13   while(amount>0)do
14   local rand = math.random(1,#valid_players)
15   local chosen = valid_players[rand]
View all 24 lines...

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

Example: assignScientists(0)

0
Thats right wjs3456 90 — 10y
Ad
Log in to vote
-1
Answered by
Yeevivor4 155
10 years ago
1function playScientists()
2    if countPlayers()<=3 then
3    assignScientists(1)

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

0
Are they within the same script? Necrorave 560 — 10y
0
Yes. They are within the same script. Yeevivor4 155 — 10y
0
Same error? (Also, instead of posting an answer, just edit your original post. Prevent confusion) Necrorave 560 — 10y
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. Yeevivor4 155 — 10y
View all comments (2 more)
0
If you message me, I may not get to it until later in the day. I am currently at work. (It's quiet) Necrorave 560 — 10y
0
Okay. Yeevivor4 155 — 10y

Answer this question