1

Why do I get the error "attempt to call a nil value"? It doesn't tell me why or where, which must mean it can't find the Dummy function. But I forward declared it, so it should see it.

local Dummy

local function Start()
  print("this line is printed")
  ErrorReport(Dummy)
  print("this line is printed")
end

local function Dummy()
  print("this line is not printed")
end

function ErrorReport(...)
  print("this line is printed")
  local ok, error = pcall(...)
  if not ok then print("Error: "..error) end
end

Start()
JohnBig
  • 97
  • 7

1 Answers1

1

local function Dummy defines a second local variable named Dummy that shadows the first one, which remains with value nil.

Just do function Dummy () ... end instead, which is equivalent to Dummy = function () ... end.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • 1
    This was explained in the answers and comments to your previous question https://stackoverflow.com/questions/71102432/why-cant-this-be-local – lhf Feb 14 '22 at 10:49
  • So when I forward declare stuff, I have to not declare the actual later implementation of the function local again? That doesn't make it global, right? – JohnBig Feb 14 '22 at 10:50
  • See my edited answer – lhf Feb 14 '22 at 10:51
  • Thanks, that answer in the other thread was only added/edited a few minutes ago, and I didn't see it. Previously it showed the upvalue function _with_ a local declaration. – JohnBig Feb 14 '22 at 10:53
  • @JohnBig, Consider deleting this question if it is answered in the previous one – lhf Feb 14 '22 at 10:54
  • Thanks, I'll delete it. So for instance by saying `local a = 5` `local a = 6` I would not change the 5 to a 6, I would create another "shadowing" a? – JohnBig Feb 14 '22 at 10:55
  • Every `local` statement creates *new* local variables. – lhf Feb 14 '22 at 10:57
  • How can it shadow if it has the same scope? Then the first `a` just becomes obsolete somehow? – JohnBig Feb 14 '22 at 10:59
  • StackExchange won't let me delete the question. But I find that it clarifies something I hadn't known. – JohnBig Feb 14 '22 at 11:06