The first error seems to have appeared by means of returning nothing from the module script. You must have forgotten to return anything from the module script being used to retrieve a value. You need to do something like the following script in order to retrieve values:
local Races = {
Race_1 = "Me'tyr",
Race_2 = "Alfr",
Race_3 = "Ztyr",
Race_4 = "Husken",
Race_5 = "Terral",
Race_6 = "Astral"
}
--converts a dictionary to an array
function toArray(dictionary)
local result = {}
--do not use ipairs
for _, v in pairs(dictionary) do
table.insert(result, v)
end
return result
end
local array = toArray(Races)
local index = math.random(1, #array)
local chosen = array[index]
print(chosen)
the module script will print it but the script cant grab it
Ok it fixes that issue but now it prints Nil instead of the value why is that? The print in the module script prints the actual value but the print in the server script prints nil
This is because the key “chosen” is not a valid key in the “RacePicker” table. You return the value as a string, not as a table. Therefore, you can just print the RacePicker variable as it holds the string.
print(RacePicker)
But if you want to pass the whole table in the module script with the “chosen” key set to random chosen string, you should be doing the following.
In the module script:
local chosen = array[index]
Races.chosen = chosen
return Races -- Return the table with the random chosen value inserted in Races
In the other script
print(RacePicker.chosen) -- Retrieve the value by indexing