title: Lua: How do I execute a callback passed from a parameter?
body: <p>I have searched for an answer to this question without luck. I have only found examples of people passing a callback into a caller function, but haven't seen how the caller actually executes the callback function code internally. If someone could provide me an example and explanation on this it would be much appreciated.</p>
  
  <p>I understand how callbacks work from a functional perspective, I've used them in other languages. I just can't figure out the syntax of how to do it right in lua. I am either executing the callback parameter wrong or passing it into the caller function wrong.</p>
  
  <p>Here is some psudocode of what I am trying to do, just a simple example so I can see it work. (I am a visual learner)</p>
  
  <pre><code>callback = function ()
      ... do stuff ...
  end
  
  function caller(callback)
      callback()
  end
  
  -- run caller function
  caller(callback)
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I have gotten a few answers saying that my example above works, and it seems like it actually does. Thank you for this, but I suppose I should get more specific considering that my actual code isn't working despite the right syntax.</p>
  
  <p>I am doing some coding for a FiveM server which utilizes essentialmode. I am attempting to use a callback in a script to clean the code up, but it doesn't seem to be firing the callback function. Here are the snippets...</p>
  
  <p>The client script:</p>
  
  <pre><code>RegisterNetEvent("parachute:callback")
  AddEventHandler('parachute:callback', function(callback)
      print("callback...")
      callback()
  end)
  </code></pre>
  
  <p>The server script:</p>
  
  <pre><code>callback = function ()
      print("callback fired")
      GiveWeaponToPed(GetPlayerPed(-1), GetHashKey("GADGET_PARACHUTE"), 150, true, true)
      SetPedComponentVariation(GetPlayerPed(-1), 5, 1, 0, 0)
  end
  
  TriggerEvent('es:addCommand', 'callback', function(source, args, user)
      TriggerClientEvent("parachute:callback", source, callback)
  end, {help = "TEST"})
  </code></pre>
  
  <p>In the above, the <code>print("callback...")</code> function executes but the <code>callback()</code> function does not. I can tell due to the print statements that I added.</p>
  
  <p>For more context to those of you not familiar with essential mode or FiveM scripting, there is a client loaded script and a server loaded script. In the server, <code>TriggerEvent('es:addCommand', ... )</code> adds a command you can execute using <code>/&lt;cmd name&gt;</code>. <code>TriggerClientEvent()</code> kicks off an event on the client via a listener created by <code>RegisterNetEvent()</code> and <code>AddEventHandler()</code>. As you can see you pass callbacks to these functions to tell them what to execute when the event is triggered. The callback function itself simply gives a parachute to my character. No parachute is given when I type <code>/callback</code> however.</p>
  
  <p>What is going wrong if it's not the syntax like I originally thought?</p>