Issue calling Local function on update function (SOLVED)

Hi,

I was following the simple “runner tutorial” with a twist of my own and i noticed that if i call a local function inside the update function of the “hero.script” i get this error:

> ERROR:SCRIPT: hero/hero.script:36: attempt to call global 'test' (a nil value)
> stack traceback:
> 	hero/hero.script:36: in function <hero/hero.script:24>

Any thoughts of what i’m missing?

1 Like

You have to ensure that:

  1. The local function is in the same file of the caller
  2. The local function is declared before the calling happen.

Just move your local function ‘test’ to be above the ‘update’ function.

2 Likes

@0angelic0 is right. The local function must be declared before the code that is calling the function. If you have two local functions that want to call each other then you can use forward-declaration of the functions:

local a

local function b()
	a()
end

a = function()
	b()
end
1 Like

Thank you that was very helpful.

1 Like