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

Checking if value already in table not working?

So I'm trying to make a random username generator, that inserts names it generated into a table and then printing them out, but only unique ones. But my problem is, despite me checking if it already is is in the table, it still adds it, no matter what.

local requirements = {"The","Dev"}
local wordList = {"Dat","Hospital","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Zero","Scripter","Builder","Scripting","Building","Logic","Test","Airport","Aviation","Military","Militia","British","German","Lufthansa"} --Strings I would like to have in my name
local takenNames = {}

local lastName

local beginning = time()
local ending

print("**********************")
print("Possibility of: ")
print((#requirements*#wordList*100).." unique combinations")
if (#requirements*#wordList*100) > 15000 then
	warn'Over 15000 combinations'
end
print("**********************")

for i = 1,(#requirements*#wordList*10) do
	local form = math.random(1,10)
	local strin = wordList[math.random(1,#wordList)]
	if form == 1 then
		lastName = "The"..strin.."Dev"
	elseif form == 2 then
		lastName = "Dev"..strin
	elseif form == 3 then
		lastName = "The"..strin
	elseif form == 4 then
		lastName = strin.."Dev"
	elseif form == 5 then
		lastName = strin.."Developer"
	elseif form == 6 then
		lastName = "The"..strin.."Developer"
	elseif form == 7 then
		lastName = strin..strin
	elseif form == 8 then
		lastName = strin.."Developer"..strin
	elseif form == 9 then
		lastName = "The"..strin..strin
	elseif form == 10 then
		lastName = "The"..strin..strin.."Developer"
	end
	if not takenNames[lastName] then --Here is where it goes wrong.
		table.insert(takenNames,lastName)
	end
	wait()
end
ending = time()
print("Done")
print("Took "..ending-beginning.." seconds") --Just testing the speed, cuz why not.

wait(5)

table.sort(takenNames) --Sort in alphabetical order

for i,v in pairs(takenNames) do --Print all the names
	print(v)
end

Additional information

No errors

Prints the following (Most recent test)

**********************
Possibility of: 
5200 unique combinations
**********************
Done
Took 17.758334229235 seconds
AirportAirport
AirportDev (x2)
AirportDeveloper (x3)
AirportDeveloperAirport (x4)
AviationDev (x4)
AviationDeveloper
BritishDeveloperBritish (x3)
BuilderBuilder (x4)
BuilderDev (x4)
BuilderDeveloper (x2)
BuilderDeveloperBuilder (x4)
BuildingBuilding (x5)
BuildingDev (x2)
BuildingDeveloper (x8)
BuildingDeveloperBuilding (x5)
DatDat (x3)
DatDev (x3)
DatDeveloper (x3)
DatDeveloperDat (x2)
DevAirport (x3)
DevBritish
DevBuilding (x3)
DevDat (x2)
DevEight (x4)
DevFive
DevLogic
DevLufthansa (x3)
DevMilitary (x2)
DevMilitia
DevNine (x2)
DevScripter (x3)
DevScripting
DevSeven
DevSix (x6)
DevTest (x2)
DevThree (x2)
DevTwo (x2)
DevZero (x3)
EightDev (x6)
EightDeveloper (x2)
EightDeveloperEight (x3)
EightEight (x2)
FiveDev
FiveFive (x2)
FourDev (x5)
FourDeveloper (x6)
FourDeveloperFour
FourFour (x5)
GermanDeveloper (x2)
GermanDeveloperGerman (x2)
GermanGerman
HospitalDev (x2)
HospitalDeveloper (x5)
HospitalDeveloperHospital
HospitalHospital (x2)
LogicDev
LogicDeveloper (x4)
LufthansaDev (x2)
LufthansaDeveloperLufthansa (x2)
LufthansaLufthansa (x7)
MilitaryDev (x2)
MilitaryDeveloperMilitary (x3)
MilitaryMilitary (x3)
MilitiaDev (x3)
MilitiaDeveloper (x2)
MilitiaDeveloperMilitia (x3)
NineDev (x2)
NineDeveloperNine
NineNine (x3)
OneDev (x3)
OneDeveloper (x2)
OneOne (x3)
ScripterDeveloper
ScripterScripter
ScriptingDev
SevenDev (x4)
SevenDeveloper (x4)
SevenDeveloperSeven
SevenSeven
SixDeveloper (x4)
SixDeveloperSix (x2)
SixSix (x2)
TenDev (x6)
TenDeveloperTen (x4)
TenTen (x5)
TestDev (x4)
TestDeveloper (x3)
TestTest (x2)
TheAirport (x2)
TheAirportAirport (x4)
TheAirportAirportDeveloper (x2)
TheAirportDev (x4)
TheAirportDeveloper (x3)
TheAviation (x6)
TheAviationAviation (x2)
TheAviationAviationDeveloper (x3)
TheAviationDev (x4)
TheBritish
TheBritishBritish
TheBritishBritishDeveloper
TheBritishDev (x5)
TheBritishDeveloper (x2)
TheBuilder (x7)
TheBuilderBuilder (x4)
TheBuilderBuilderDeveloper (x5)
TheBuilderDev (x5)
TheBuilderDeveloper (x3)
TheBuilding (x3)
TheBuildingBuilding (x3)
TheBuildingDev (x2)
TheBuildingDeveloper (x3)
TheDat
TheDatDat
TheDatDatDeveloper
TheDatDev (x2)
TheDatDeveloper (x4)
TheEight
TheEightDev (x2)
TheEightDeveloper (x3)
TheEightEightDeveloper (x4)
TheFive (x4)
TheFiveDev
TheFiveFive (x3)
TheFiveFiveDeveloper
TheFour (x2)
TheFourDev (x7)
TheFourDeveloper
TheFourFour (x2)
TheGermanDev (x4)
TheGermanDeveloper (x3)
TheGermanGermanDeveloper
TheHospital (x2)
TheHospitalDev
TheHospitalDeveloper
TheHospitalHospital (x3)
TheLogic
TheLogicDev (x3)
TheLogicDeveloper
TheLogicLogic
TheLogicLogicDeveloper
TheLufthansa
TheLufthansaDev
TheLufthansaLufthansa
TheLufthansaLufthansaDeveloper (x2)
TheMilitary (x2)
TheMilitaryDev (x2)
TheMilitia (x2)
TheMilitiaDev
TheMilitiaDeveloper (x5)
TheMilitiaMilitia
TheNineDev (x3)
TheNineDeveloper (x7)
TheNineNine
TheNineNineDeveloper
TheOne (x2)
TheOneDev (x2)
TheOneOne (x2)
TheOneOneDeveloper
TheScripter (x4)
TheScripterDeveloper (x2)
TheScripterScripter
TheScripterScripterDeveloper (x4)
TheScripting
TheScriptingDev (x2)
TheScriptingDeveloper
TheScriptingScriptingDeveloper (x2)
TheSevenDev (x3)
TheSevenDeveloper
TheSevenSeven (x5)
TheSevenSevenDeveloper (x3)
TheSixDev (x2)
TheSixSix
TheSixSixDeveloper (x3)
TheTen (x2)
TheTenDev (x4)
TheTenDeveloper
TheTenTenDeveloper (x3)
TheTestDeveloper (x5)
TheTestTest (x2)
TheTestTestDeveloper (x2)
TheThree
TheThreeDev (x3)
TheThreeThree
TheThreeThreeDeveloper (x3)
TheTwoDev (x3)
TheTwoDeveloper
TheTwoTwoDeveloper
TheZero (x3)
TheZeroDeveloper (x2)
TheZeroZero (x2)
TheZeroZeroDeveloper (x2)
ThreeDev
ThreeDeveloper (x4)
ThreeDeveloperThree (x4)
ThreeThree
TwoDev (x4)
TwoDeveloper
TwoDeveloperTwo (x3)
ZeroDev (x3)
ZeroDeveloper (x2)
ZeroDeveloperZero (x2)

1 answer

✓ Accepted answer

Suppose lastName is "BlueTaslem".

First, you check if takenNames["BlueTaslem"]. Since this name is new, the if continues on.

We insert "BlueTaslem" into takenNames. That table.insert puts it at index takenNames[1], not at index takenNames["BlueTaslem"].

You can fix this by replacing table.insert(takenNames,lastName) with takenNames[lastName] = true.

You can build a separate list of names either as you go (by table.insert(nameList, lastName)) or at the end (by looping over for name in pairs(takenNames) do)


You shouldn't use randomness from this; you clearly want to generate all possibilities.

You shouldn't use a big elseif tree; you should use a list of "patterns". Your patterns are simple and could be written as a string. I chose to use @ to denote the blank to fill in.

local words = {
	"Dat","Hospital","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Zero","Scripter","Builder","Scripting","Building","Logic","Test","Airport","Aviation","Military","Militia","British","German","Lufthansa"
}

-- There are many ways to specify the "patterns", but this is the easiest
local patterns = {
	"The@Dev",
	"Dev@",
	"The@",
	"@Dev",
	"@Developer",
	"The@Dev",
	"@@",
	"@Developer@",
	"The@@",
	"The@@Developer",
}

local names = {}
for _, word in pairs(words) do
	for _, pattern in pairs(patterns) do
		-- replace all of the @-signs in the 'pattern' with 'word'
		local name = pattern:gsub("@", word)
		table.insert(names, name)
	end
end

table.sort(names)

You shouldn't need to check for duplicates at all for the given combination of patterns / words because you can't get the same name if you use a different pattern or a different word.

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