Today, I started working on my first module script for basic math functions. So far, I have:
A function to validate data (To make sure that it's just an array of numbers) A function for summation and A function to get the average/"mean" of the data.
Here's the script:
local module = {}
module.valid = function(data)
if type(data) == "table" then
for _, v in pairs(data) do
if type(v) ~= "number" then
assert(false, "Data must only contain number values.")
return false
end
end
return true
end
end
module.summation = function(data)
local sum = 0
if module.valid(data) then
for _, v in pairs(data) do
sum = sum + v
end
end
return sum
end
module.average = function(data)
if module.valid(data) then
local sum = module.summation(data)
local avg = sum/#data
return avg
end
end
return module
With this, I can call both the validation and the summation functions from my command line without having to start the game. However, when I do...
print(require(game.ServerScriptService.Math).average({1, 2, 3, 4, 5, 6}))
...I get this error:
19:58:51.390 - print(require(game.ServerScriptService.Math).average({3, 4,:1: attempt to call field 'average' (a nil value)
When I run the game, I can do the exact same command and get (3.5). Could someone explain to me what's happening here?
