Why am I getting this error?

I continuedly keep getting this error, and I don’t understand why.

 14:48:03.446  ServerScriptService.nameGen:6: attempt to call a nil value  -  Server - nameGen:6
  14:48:03.446  Stack Begin  -  Studio
  14:48:03.446  Script 'ServerScriptService.nameGen', Line 6  -  Studio - nameGen:6
  14:48:03.446  Stack End  -  Studio

It happens whenever I call a function in this module.

local module = {}

local v = { -- vowels
	"a",
	"e",
	"i",
	"o",
	"u",
	"y"
}

local c = { -- constants
	"b",
	"c",
	"d",
	"f",
	"g",
	"h",
	"j",
	"k",
	"l",
	"m",
	"n",
	"o",
	"p",
	"q",
	"r",
	"s",
	"t",
	"v",
	"w",
	"x",
	"y",
	"z"
}

function createName(name)
	local first = " "
	local second = " "
	local third = " "
	local fourth = " "
	local fifth = " "
	local sixth = " "
	local seventh = " "
	
	first = c[math.random(#c)]
	second = c[math.random(#c)]
	third = c[math.random(#c)] or v[math.random(#v)]
	fourth = c[math.random(#c)] or v[math.random(#v)]
	fifth = c[math.random(#c)]
	sixth = c[math.random(#c)]
	seventh = c[math.random(#c)]
		
		
	local order = {first, second, third, fourth, fifth, sixth, seventh}
	
	name = table.concat(order)
	
	return name
end

return module

1 Like

I will assume that you are trying to call the “createName” function. This function is local to the module script itself and you had never exposed it as a function of the module, so it doesn’t exist in it hence why the error is thrown. You probably meant to do this:

function module.createName()
   -- code
end
2 Likes

After I return “name” when the function ends, how am I supposed to print what the function did in the module from the script that fired the function in the module?

1 Like

Which part of what the function did? Other scripts don’t have access to anything in the module unless you specifically give them access. You’re returning name and that should be all you need because that’s all the function does.

This bit right here won’t work either unless for some reason c[math.random(#c)] is nil, you’ll need to merge the two tables if you want to get a letter or vowel in a single line.

And lastly, you’re not showing us the error. ServerScriptService.nameGen (line 6) is not this script. You’ll need to include more details for better help, but as it stands, @rare_tendo has the likely answer. You don’t return the function in the module and so your script doesn’t have access to it.

1 Like

If you’re confused how to return and call functions in a module, it should be formatted like this:

local module = {}
function module.CreateName()
    return "hi"
end
return module
--Script
local nameModule = require(--where module is located)
local name = nameModule.CreateName()
print(name) --outputs "hi"
2 Likes