Help with calling a module function

I have a server script and a module script named “Functions” with a function that I want to call, but every solution I try results in the server script throwing the error “ServerScriptService.main:4: attempt to call a nil value”. Why would this be? I can’t figure it out. (The function is supposed to return a random number between 1 and the maximum, but never the same number twice in a row.)

local Functions = require(script.Parent:WaitForChild("Functions"))


print(Functions.rng(2))
local module = {}

local number = 0
local previousnumber = 0

function rng(max)	
	number = math.random(1,max)

	if number == previousnumber then
		return rng(max)
	else
		previousnumber = number
		return number
	end
end

return module

Here’s a fix:
function rng(max)function module.rng(max)

As far as I can tell your code, your old code is basically calling a nil value. What I’m saying about calling a nil value is the script attempts to call a non-existent function in the table. You should try printing out what the module returns for a more visual understanding. :wink:

You just need to make sure the function is part of the module. There are multiple ways of doing this.

Example 1.

function module:rng(max)

Example 2:

function module.rng(max)

Example 3:

module.rng = function(max)

For whatever example you use, make sure to use the correct punctuation that is present (: or .) when calling the function

3 Likes

Thanks, now it’s working perfectly! It’s been a while since I’ve coded, can’t believe I forgot lol