The error I'm getting 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:
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);
I don't see where _2syltest
is being assigned -- only syltest
. If _2syltest
isn't coming from somewhere else, that could be the issue, since that's the condition in your if uses that value.
From the structure of your code it seems that you wanted to use syltest < 50
in both if
and elseif
conditions, instead you used _2sylchoice < 50
. Moreover probably you meant _2sylchoice = _2syl1
(a typo there?).
See if this is what you meant:
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
As an aside, you are using way too much global variables. All the variables you use should be local ones unless you have a strong reason to do otherwise (although I didn't change this aspect of your code in order not to confuse you in the case you weren't aware of what I'm talking about).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.