RegisterNUICallback not working

I’m might have done it all wrong, but what I’ve done is I’ve made a lua script which opens the menu when I press E, it works without any problems. When I need to close the menu I can’t use the ingame controls to get the key so I’ve used document.body.onkeydown in my JS which also works and it’s closing the menu (setting the display to none) but the problem is I doesn’t disable SetNuiFocus.
I’m trying to use
$.post(‘http://testing/event’, JSON.stringify({}));
I’m not sure that’s the right URL.
the RegisterNUICallback is located in testing/client.lua

Anyway I’m trying to use it to trigger this

RegisterNUICallback("event", function(data, cb)
      SetNuiFocus(false,false)
      cb()
  end)

I have no idea how to use the callback thing, I would appreciate it if you could explain it a bit.

That part looks fine. Please post the whole JS code for triggering that POST method.

RegisterNUICallback("closeadmin", function(data, cb)
    SetNuiFocus(false, false)
    cb("ok")
end)
2 Likes
document.body.onkeydown = function(e){
        if (e.keyCode == 69) {

          var t = document.getElementById("test");
          t.style.display = "none";
          $.post('http://testing/event', JSON.stringify({}));
        }
    };

Are you sure that onkeydown is being triggered?

1 Like

I mean the display attribute of the test element gets set to none, so I guess it would have to be

Hm. testing is your resource name?

1 Like

it is. inside the testing folder is a the __resource.lua and the client.lua and then there’s another folder called nui and that’s where I have the css ,js and html.
The only thing I can think of is that the URL is wrong

        CloseMenu() {
            this.showMenu = false;
            this.$refs.depositForm.reset();
            this.$refs.withdrawForm.reset();
            axios.post("http://" + this.resource_name + "/closeatm", {
            }).then( (response) => {
                console.log(response);
            }).catch( (error) => {
                console.log(error);
            })
        },

basically my URL is http://xrplife/closeatm

I understand that I am not using a JQuery post its just the best I got since I use AXIOS…

1 Like

Well, I’ve got to go. Hopefully I’ll look at it tomorrow again and it will all make sense

Yeah. Sleeping on it always helps. Good luck!

1 Like

I’ve been trying stuff for a while now. the nui callback never gets called.
Am I understanding this right?
$.post(‘http://testing/event’, JSON.stringify({}));
this should run whatever is in the closeGui() function right?

RegisterNUICallback('event', function(data, cb), {
    closeGui()
    cb('ok')
    }
end)

Remove that comma afterward.

Also, I am not sure if Lua allows the use of { } for functions. I think it tries to interpret it as a table. I could be wrong about that. I haven’t used Lua recently.

Try this and see if it works:

RegisterNUICallback('event', function(data, cb)
    closeGui()
    cb('ok')
end)
1 Like

Edit nvm :smile:

20 chars

1 Like

I forgot to change it back to
RegisterNUICallback('event', function(data, cb) closeGui() cb('ok') end)
before making the post.
I was already doing this and it’s not working

Ah… I didn’t read above, where you already tried like that.

When I did use Lua, I never used the cb, and it worked fine for me. Try without maybe?:

RegisterNUICallback('event', function(data)
    closeGui()
end)

Another thing to maybe try: Rename the event to something different and more unique. It could be some sort of conflict on the backend.

1 Like

Still doesn’t work, tried both of those things

Are there any errors in your F8 console? Try putting console.log("hello") after the $.post line to see if it is getting that far. Does your resource name/folder have a capital letter in it?

2 Likes

Thank you, the problem was a capital letter in the resource name, when I first wrote the post the name was testing but later I changed it to testMenu. I would have told you I changed the name but didn’t think that it mattered.
Well, it seems to work now

1 Like

Hi I have a problem because when I turn on the tablet, it grinds the screen

RegisterNUICallback(‘NUIFocusOff’, function()
stopTabletAnimation()
SetNuiFocus(false, false)
SendNUIMessage({type = ‘close’})
PoliceGUI = false
end)