NameGen () function: attempt to compare nil with number

The error I get is

[string "function NameGen()..."]:14: attempt to compare nil with number stack traceback: [string "function NameGen()..."]:14: in function 'NameGen' [string "function NameGen()..."]:23: in main chunk [C]: ? 

My code is:

 function NameGen() preftest = math.random(100) ; syltest = math.random(100) ; sufftest = math.random(100) ; pref1 = "New "; _1syl1 = "Lon"; _2syl1 = "Don"; suff1 = " City"; prefchoice = pref1; _1sylchoice = _1syl1; _2sylchoice = _2syl; suffchoice = suff1; if preftest < 50 and _2syltest < 50 and sufftest < 50 then cityname = prefchoice .. _1sylchoice .. _2sylchoice .. suffchoice; elseif preftest < 50 and _2syltest < 50 then cityname = prefchoice .. _1sylchoice .. _2sylchoice; else cityname = _1sylchoice; end end NameGen(); print(cityname); 
+4
source share
2 answers

I do not see where _2syltest is assigned - only syltest . If _2syltest does not come from somewhere else, this may be a problem, since this condition uses this value in your case.

+5
source

From the structure of your code, it seems that you wanted to use syltest < 50 in the conditions of if and elseif , instead you used _2sylchoice < 50 . Moreover, perhaps you meant _2sylchoice = _2syl1 (is there a typo?).

See what you mean:

 function NameGen() preftest = math.random(100) syltest = math.random(100) sufftest = math.random(100) pref1 = "New " _1syl1 = "Lon" _2syl1 = "Don"; suff1 = " City" prefchoice = pref1 _1sylchoice = _1syl1 _2sylchoice = _2syl1 suffchoice = suff1 if preftest < 50 and syltest < 50 and sufftest < 50 then cityname = prefchoice .. _1sylchoice .. _2sylchoice .. suffchoice elseif preftest < 50 and syltest < 50 then cityname = prefchoice .. _1sylchoice .. _2sylchoice else cityname = _1sylchoice end end for i = 1, 100 do NameGen() print(cityname) end 

Aside, you are using too many global variables. All the variables you use should be local unless you have a good reason for this (although I did not modify this aspect of your code so as not to confuse you if you did not know what I'm talking about).

0
source

Source: https://habr.com/ru/post/1501979/


All Articles