Having trouble with Javascript Natives | Uncaught ReferenceError: GetPlayerPed is not defined

I am relatively new to JS.
The HTML side of the JS (The menu functions) is working fine.
I simply want to use Natives in JS, instead of in Lua because I don’t want to trigger a NUICallback every time I update a value.

Upon trying to use a FiveM Native in my JS;

  $("#test-button").on("click", function () {
    const ped = GetPlayerPed();
    console.log(ped);
  });

I am met with an error in the console:
image

fxmanifest:

fx_version 'cerulean'
game 'gta5'
lua54 'yes'

ui_page 'html/index.html'

shared_scripts {
    'config.lua',
}

server_script 'server/server.lua'

client_scripts {
    'client/main.lua',
    'client/cl_functions.lua',
    'client/cl_client.lua',
    'html/js/script.js',
}

files {
    'html/**/*.css',
    'html/**/*.html',
    'html/**/*.js'
}

If anyone knows what I might be missing/doing wrong, please let me know!
Cheers, CClark

Solution: Run the natives in a separate JS file that isn’t linked to the UI.
JS files linked to the UI (NUI), simply cannot use natives.
Javascript is global scope so passing variables between files won’t be an issue.
I was not missing any server files.

Hi, the problem you’re having is that you are trying to call a native within NUI. NUI doesn’t really have knowledge of the game. You can, however, pass data to NUI using SendNUIMessage iirc.

1 Like

Thank you for your response.
How do I go about removing it from NUI?
and will I still be able to control the HTML if I do?

I’ve added my fxmanifest to the original post if that is related.

Found out that javascript is global scope, so I will use another JS file (That isn’t linked to the UI) to run the natives

Thank you @zee! Much appreciated.

1 Like