How to make a randomized issuing city?

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.

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";

}

without having to write 50 thousand if statements.

Best regards,
anyone who sees this lol

1 Like

Is the Toyuma, Reussi, Arsoftika, Frenicia, Fed. Rep. of Stalm the countries?

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.)

You can use math.random to pick a random thing in the table, and # to get the number of things in a table.

local ChosenCity = ChosenCountry[math.random(#ChosenCountry)]

ChosenCountry would be the country you have picked, you can’t use this method with the countries because its not a table, its a dictionary.

uh so how do I get randomized values from a Dictionary?

Alright. I’ve truly just looked at another post with the phrase “randomized values from a Dictionary” and I’ve now acquired what I needed.

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

Can you send me the whole script?

Countries = {
	"Reussi",
	"Iraqianai",
	"Arsoftika",
	"Frenicia",
	"Fed. Rep. Of Stalm",
	"Toyuma"
}

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"};
}

WrongCities = {
	["Toyuma"] = {"Saliam", "Menizxo", "Satalmia"};
	["Reussi"] = {"Northern Reussi", "Better Reussi", "Old Reussi"};
	["Arsoftika"] = {"Worst Reussi", "Arstotzka", "Jerma"};
	["Frenicia"] = {"Guytona", "Ram", "France"};
	["Fed. Rep. Of Stalm"] = {"Islam", "Oldernia"};
}

local function RandomCityFor(country, array)
	CountryArray = array[country]
	NewCity = CountryArray[math.random(1, #CountryArray)]
	
	return tostring(NewCity)
end


Genders = {
	"Male",
	"Female" -- very political indeed. but i cant say "sex" or i will be shot by the robloxians at the roblox HQ
}

-- this is all redacted because no steal

return module

its a module that makes npcs spawn with data. i just removed one of the functions for redacting-ness

What’s the problem with the script?

Well its solved now. Thank you for helping, although!

1 Like
IssuingCountries = {"Toyuma", "Reussi", "Arsoftika", "Frenicia", "Fed. Rep. Of Stalm"}

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 getRandomCountry()
	return IssuingCountries[math.random(#IssuingCountries)]
end

local function getRandomCity(country)
	return IssuingCities[country][math.random(#IssuingCities[country])]
end

local RandomCountry = getRandomCountry()
local RandomCity = getRandomCity(RandomCountry)
print(RandomCountry, RandomCity)

Here’s how I’d go about this.

image

1 Like