I am trying to figure out how to create a randomized issuing city that is inside a randomized country. I have already made the randomized country, but I’m not the best in arrays/tables, and I’ve tried a little bit at making it show the cities inside the countries be randomized.
Yes. They are the countries. The other ones are the issuing cities for those countries. (Also to let you know its for a game that im making similar to Papers Please.)
Sorry it took so long, i had to test in in Roblox Studio. Anyways, here is the code:
local IssuingCities = {
["Toyuma"] = {"Jaliam", "Meniexa", "Salamia"};
["Reussi"] = {"Northern State", "Greater Reussi", "Grand Reussi"};
["Arsoftika"] = {"Better Reussi", "Arkosta", "Jerfina"};
["Frenicia"] = {"Daytona", "Rei", "Freancies"};
["Fed. Rep. Of Stalm"] = {"Islamai", "Modernea"};
}
local function getRandomizedCityForCountry()
local countryWithIssuingCity = {}
local function findRandomCity(array)
local randomNum = math.random(1,#array)
local ChosenCity = array[randomNum]
if ChosenCity ~= nil then
return ChosenCity
end
end
for country,arrayOfCities in pairs(IssuingCities) do
local city = findRandomCity(arrayOfCities)
if city ~= nil then
countryWithIssuingCity[tostring(country)] = tostring(city)
end
end
return countryWithIssuingCity
end
local countryPairTable = getRandomizedCityForCountry()
for i,v in pairs(countryPairTable) do
print(i.."="..v)
end
Uh. Sorry if I’m being mean for some reason lol, but I got some inspiration from your function and…
local function RandomCityFor(country, array)
CountryArray = array[country]
NewCity = CountryArray[math.random(1, #CountryArray)]
return tostring(NewCity)
end