Trying to check if a string is a country

  1. What do you want to achieve? I want to get one single country in this table with all countries

  2. What is the issue? I used this local function

local function getc()
	
	local module = require(game.Workspace.path) -- giant table with all countries 
	for i,v in pairs(module) do -- table of all countries (es. TJ = {Name = "Tajikistan"})
		for i,c in pairs(v) do  -- single country 

			return c
		end

	end
end

and with this print: print(getc()), it printed me only one of the countries (totally random) →
image

All answers are welcome :D, ask me for more information if you don’t understand

It looks like you’re returning, which is going to return c and halt the loop. To further illustrate my point, here is an example:

local Example = {1, 2, 3, 4, 5, 6}



local function PrintNumbersInTable()
	for i, v in pairs(Example) do
		return v
	end	
end

local function PrintTableOfNumbers()
	for i, v in pairs(Example) do
		print(v)	
	end
end
print(PrintNumbersInTable()) -- prints 1 value.
PrintTableOfNumbers() -- prints all values in the table, as we're not returning out of the function.

So, what exactly does this mean for you? It depends on your use case here. If you intend to just print every country, you can simply print inside the function without returning anything, or return a table of all countries. If you wanted to check information on a certain country, you could simply look it up in the dictionary through a passed variable in the function like so:

local dict = {
	TJ = {
		Hello = "1",
		World = "2"
	},
	
	CA = {
		Hello = "3",
		World = "4"
	}
}



local function GetHelloValueForValue(Value)
	if dict[Value] then
		
		return dict[Value]["Hello"]
	end
end

print(GetHelloValueForValue("CA")) -- prints 3
1 Like

Ty for the answer

i understand the problem, the loop is really long and it takes very time for end; i put the loop in the function, but basically it breacks the script.

Basically i have to wait that the loop finished first to take up again the script
image

i can try use spawn(function()

Edit: maybe i have to put the loop outside the function

Hmm, what exactly is the desired outcome? Are they entering a countries name or something? There may be easier ways to achieve what you’re trying to do.

1 Like

basically what i am doing is a voting system with HTTP service; i want that if a player write a country in the textbox then i can see it on ds. I just want to be sure that what a player is typing is a country

Ahh alright. Have you tried something like this? You can loop through the table and check if the country value is equal to the entered value, like this:

local function CheckIfCountry(EnteredValue)
	for i, v in pairs(ExampleModule) do
		if v["Country"] == EnteredValue or v["Country"] == string.gsub(EnteredValue, " ", "") then -- since it looks like your table doesnt have spaces, you can prob use gsub to erase spaces if the client enters that
			return v["Country"] -- is a country, retruns the countries name in the table 
		end
	end
end

someButton.MouseButton1Click:Connect(function()
	local TextBoxText = someTextBox.Text
	local Country = CheckIfCountry(TextBoxText)
	if Country then -- anything below this wont fire if its not a country in the table
		-- your code to datasave would be here, debounce might be useful on server side if ur using datastores as well as client sided stuff
	end
end)
1 Like

mhh. i try it rn because i did my script in a different way

1 Like

I solved in this way:

i put a folder in workspace and for each country i put a boolvalue using this script

for i,v in pairs(require(game.Workspace.path)) do
	for i,c in pairs(v) do
		local va = Instance.new("BoolValue", game.Workspace.countries)
		va.Name = c
		end
	end

then

in the script i put if folder:finfirstchild(suggestion) then...

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.