Why is print() printing 0

local BoyGirlNames = {
["BoyNames"] =  {"Liam","Noah","William","James","Oliver","Benjamin","Elijah","Lucas","Mason"
	   };

["GirlNames"] = {"Emma","Olivia","Ava","Isabella","Sophia","Charlotte","Mia","Amelia","Harper","Evelyn"
	}
}

print(#BoyGirlNames)

I have this code but even though there is other tables inside BoyGirlNames print(#BoyGirlNames) prints 0

When I do this it prints 2.

local BoyGirlNames = {
1,
2,
[“BoyNames”] = {“Liam”,“Noah”,“William”,“James”,“Oliver”,“Benjamin”,“Elijah”,“Lucas”,“Mason”
},

[“GirlNames”] = {“Emma”,“Olivia”,“Ava”,“Isabella”,“Sophia”,“Charlotte”,“Mia”,“Amelia”,“Harper”,“Evelyn”
}
}

print(#BoyGirlNames)

The variable: ‘BoyGirlsName’ are assigned as this but you have included as ‘#BoyGirlsName’, which the hashtag is causing you the error

Not exactly, typing an hashtag before the table’s name will return the amount of entries inside of it.

local foo = {1, 2, "hi"}
print(#foo) --Returns 3

The issue OP has is the wrong number of entries being returned.

1 Like

@jrelvas Ohh ok, I’m new to this Roblox Programming, that probably why, but thanks for correcting me!!! :grin:

1 Like

The problem is pretty obvious your just referring to the table that holds the BoyNames and GirlNames, you would have to do something like this.

local BoyGirlNames = {
["BoyNames"] =  {"Liam","Noah","William","James","Oliver","Benjamin","Elijah","Lucas","Mason"
	   };

["GirlNames"] = {"Emma","Olivia","Ava","Isabella","Sophia","Charlotte","Mia","Amelia","Harper","Evelyn"
	}
}

print(#BoyGirlNames.BoyNames,#BoyGirlNames.GirlNames)
1 Like

The # operator doesn’t account for dictionaries as opposed to an array.

4 Likes

thanks for the answer. How can I make it pick random from the boy names or the girl names.

You could also do something like this for the printing

EDIT : Nevermind sorry this doesnt work

local BoyGirlNames = {
["BoyNames"] =  {"Liam","Noah","William","James","Oliver","Benjamin","Elijah","Lucas","Mason"
	   };

["GirlNames"] = {"Emma","Olivia","Ava","Isabella","Sophia","Charlotte","Mia","Amelia","Harper","Evelyn"
	}
}

local tab,value = next(BoyGirlNames.BoyNames)
local tab2,value2 = next(BoyGirlNames.GirlNames)
print(tab,#value)
print(tab2,#value2)
local RandomNumber = math.random(1, #BoyGirlNames.BoyNames) --Choose a random number between 1 and the number of boy names
local BoyName = BoyGirlNames.BoyNames[RandomNumber] --Get boy name with that specific number.
2 Likes

It depends. Do you want it to pick 50% boys and 50% girls? Or do you want a fair chance for each?
With 50/50, if you have only two girl names, there is a 25% chance of getting “Emma” but only a 50%*10% = 5% chance of getting “Liam”

50/50 is easy.

if math.random()<.5 then
    return BoyGirlNames.BoyNames[math.random(#BoyGirlNames.BoyNames)]
else
    return BoyGirlNames.GirlNames[math.random(#BoyGirlNames.GirlNames)]
end

otherwise, I would advise keeping a separate table with combined names, or just doing it differently. It depends on your use-case, but take a look at this way of organizing. I can come up with some better ones if you tell me all the ways you need to use this data.

BoyGirlNames = {
    {"Sophia", "Girl"},
    {"David", "Boy",}
    {"Jordan", "idek"}
}
1 Like