.lua file not allowing multiple bits of information

I have a file called “Data Storage.lua” which is a lua file in which i intended to store data needed by multiple scripts within my program.

local playerHealth = {} -- creates a variable
playerHealth.my_value = 12
return playerHealth -- returns playerHealth to scripts that require

I wanted to add another variable to this to track enemy health, but if I try to do that in the same way as the previous one (I changed my_value to enemy_value cause i thought that would be the correct way to do it)

local enemyHealth = {} -- creates a variable
enemyHealth.enemy_value = 12
return enemyHealth -- returns playerHealth to scripts that require

it spits out this error.

You can’t return multiple times out of a module. You just return the whole thing once.

The standard module format is:

local M = {}

M.playerHealth = {}
M.playerHealth.my_value = 12

M.enemyHealth = {}
M.enemyHealth.enemy_value = 12

return M

You would access like so:

local dataStorage = require "path.to.dataStorage"
print(dataStorage.playerHealth.my_value)

6 Likes