How would I get something from a module?

If I require a module from a local script, lets say this is the contents of the module

local module {
["Potato"] = "rbxassetid://potato"
}
return module

How would I get potato and its rbxassetid from the module in the local script?

local required_module = require(your module)

required_module.Potato

1 Like

So by doing this, to an image label, will it turn the image label to the potatos assetid?

If the id is correct yes, something like this:

ImageLabel.Image = yourModule["Potato"]

Have you ever seen the proocountry module? if not then it goes like the lines of this

local countries = {
	['US'] = { name = 'United States', decal = 'rbxassetid://6764359716' },
}

local getCountry = function(code)
	return countries[code]	
end

return {
	countries = countries,
	getCountry = getCountry
}

If the players country is the US how can I get that table into the image?

First you need the player’s country and then:

local country = "US"
local name = countries[country].name
local decal = countries[country].decal
1 Like

Gives me an error, attempted to index nil with 'name

This error means that there is nothing called “name”. From the example above it’s working for me:

local countries = {
	['US'] = { name = 'United States', decal = 'rbxassetid://6764359716' },
}

local country = "US"
local name = countries[country].name
local decal = countries[country].decal
print(name)

Except it isn’t a table in the local script, its in a module.
It returns me “attempted to index nil with name” when the example listed has the same name

local LocalizationService = game:GetService("LocalizationService")
local player = game.Players.LocalPlayer
 module = require(workspace.ProoCountry)

local result, code = pcall(function()
	return LocalizationService:GetCountryRegionForPlayerAsync(player)
end)

if result then
	local flag = code
	local name = module[flag].name
	local decal = module[flag].decal
	else
	print("GetCountryRegionForPlayerAsync failed: " .. code)
end

It’s the same thing as it returns a table from the module:

Script:

local countries = require(script.ModuleScript)

local country = "US"
local name = countries[country].name
local decal = countries[country].decal
print(name)

Module:

local module = {
	['US'] = { name = 'United States', decal = 'rbxassetid://6764359716' },
}

return module

It seems in my code, it just doesn’t work that way. I tried without the localizations and it just gave errors.

I’ll just create a new module based off the info you gave me without the decals name parts and just the assetid.
Thanks

1 Like

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