[Help] .lua net event getting received by a .js?

Hey guys! I’ve been working on a script that disables HUD until the player spawns. (Using ESX kashacters multiple character support)

It’s working great everywhere I have lua. However I have some status bars for health that are using javascript. I tried disabling them from the lua side and that didnt work.

So my question is, can i trigger a function is java from a lua script? Could someone give me a quick example? Thanks guys I hope I can help someone else out one of these days.

Since you are talking about HUD bars, do you mean JavaScript as in NUI?

lua:

SendNUIMessage({
    display = false
  })
SendNUIMessage({
    display = true
  })

js:

$(function() {
	window.addEventListener('message', function(event){
		var item = event.data;
		if (item.display) {
			$('#item').show();
		} else {
			$('#item').hide();
		}
	}
})
1 Like

Yes, I didnt realise there was a different name for it but yes that’s what I meant

Thanks so much for this! I will check it out after work. I think I will be able to fix what I’ve been struggling with.

Hey one more question for you.

If I name a function the same thing can I trigger all 3 (assuming I have 3 named the same thing) at once?

Example:
My disable HUD before playerspawn event. Can I put functions in each script that has HUD called “DisableHUD” and then just use one trigger to call them? Currently I have 3 seperately named functions and it works great but I was just wondering.

Thanks again guys!

You should not have functions with the same name, I’m not sure lua will even allow you.

To achieve this do the following:

RegisterNetEvent('yourEvent')

Then rather than having the function 3 times, you can do this for each:

AddEventHandler('yourEvent', function(args)
    -- do some shit here
end)
1 Like

Oh I didnt realise you could do that!

Thanks so much James I will report back tonight if I can get the status bars sorted!

For example, my framework sends out character information when the player selects a character using

TriggerEvent('fsn_main:character:Init', char)

Which is then picked up in our health and hunger script, which is something like this

local init = false
AddEventHandler('fsn_main:character:Init', function(char)
    init = true
end)

Where do you register your net event? Does it matter?

It has to be registered before it’s ran, so I just register it before I use TriggerEvent

Oh excellent! I’m going to make these changes tonight. Should make it more uniform and I can call the same event in the future if I add more HUD

This all started cuz I didnt like how it showed HUD when you were picking your character.

1 Like

same. lol.

Well here’s the NUI code I’m dealing with. I’ve tried adapting it a few different ways and the only result I get is disabling the status bars completely. Is there a way that you see to easily disable it? What’s funny is it already has a disable variable on the lua side. The problem is that it’s broken. How it works is that it checks if the pause menu is open, and if it is it passes a show = false. But the issue is it just flashes rapidly when the pause menu is up.

So long story short I could use that, but then it would just flash rapidly like it already does… LOL I wish I knew more about this. Especially because I’d like to start making new menus using NUI and forms…

Anyways if you could give me your two cents I’d appreciate it! :smiley:

    window.addEventListener('message', function(event) {
        var data = event.data;
        $(".container").css("display",data.show? "none":"block");
        $("#boxHeal").css("width",data.health + "%");
        $("#boxStamina").css("width",data.stamina + "%");
        if (event.data.action == "updateStatus") {
            updateStatus(event.data.st);
        }
    })
})