upvote
Also, it’s possible to force global variables to be explicitly declared.

  function declare(name)
    rawset(_G,name,false)
  end
  function exists(name)
    if rawget(_G,name) ~= nil then 
      return true 
    else return false end
  end
  setmetatable(_G, {
    __newindex = function(self,x) error("Undeclared global " .. x) end,
    __index = function(self,x) error("Undeclared global " .. x) end
  })
And now

  declare("foo")
  foo = 1
  -- Create name if it hasn’t 
  -- been declared yet
  if not exists("name") then 
    declare("name")
    name = 1
  end
More discussion: https://lua.org/pil/14.2.html
reply