[HELP] Callbacks as arguments in events

In the wiki page of the TriggerEvent() function there is an example for the callbacks usage in events:

-- example of callbacks
local printFunction = Citizen and Citizen.Trace or print

TriggerEvent('myEventWithACallback', function(argument)
    printFunction(tostring(argument))
end)

But it happens not to be enough extensive to be able to really understand its usage, or at least that’s my case. Would someone please, either explain a bit how it actually works or show a working example of its usage, or both (that’d be perfect)? I mean, I understand a bit how it works, but I’m trying to get it to work but no luck yet, o that’s why I’m asking for help…

This is what I’ve tried doing so far, without results:

RegisterServerEvent('event')
AddEventHandler('event', function(cb) cb() end) -- also tried doing just cb instead of function(cb) cb() end, but this makes more sense to me, though neither of them work.

TriggerEvent('event', function()
    print('foo')
end)

Any help would be appreciated, thanks.

so like

RegisterServerEvent("Test:Event")
AddEventHandler("Test:Event", function(callback)
   print("Event Triggered")
   callback("Result Message")
end)

TriggerEvent("Test:Event", function(results)
   print("Callback Results: " .. results)
end)

See what messages it prints out?

2 Likes

It doesn’t print anything for me…

1 Like
1 Like

But what that does, is just send it to the server and send it back to the client, and execute the callback there. I want to send callback to the server and execute it in the server.

I’m not sure I understand you? You can’t transfer a function over the network, if it exists on the client the only place to trigger it is on the client. So you tell the client to invoke the function by sending back the handle and any parameters. Even if it the syntax was a little cleaner it would still just be sugar for that.

Oh, damn. Thanks tho