MaxHealth detection and how to do it effeciently

Currently, Im using Humanoid.HealthChanged:Connect(function() To detect when the maxhealth changes (It works for both health and maxhealth) however, Im doing this to set the textof a textlabel

In order to do this, Im using a bindable. Having to fire it whenever the health changes and the maxhealth changes is unnecessary networking.

Anyone have any idea how to make this performant? I mean it DOES work but its not good for network.

PS: :GetPropertyChangedSignal() Doesnt work for some reason.

you could use :GetPropertyChangedSignal() to only listen when MaxHealth changes.

3 Likes

Use: humanoid:GetPropertyChangedSignal(“MaxHealth”):Connect(function() :smile:

3 Likes

Doesnt work I have no idea why I already tried using it but it just wont signal

As I said, Ill put it in the post I guess

you must be doing something wrong then.
Do you mind sharing the code?

Yea no problem.

Im using Knit Framework btw, its the same as using localized variables.

	Knit.Player.Character.Humanoid:GetAttributeChangedSignal("MaxHealth"):Connect(function()
		
		local Humanoid: Humanoid = Knit.Player.Character.Humanoid
		local MaxHealth = Humanoid.MaxHealth
		print(MaxHealth)
		
		MaxHealthBindable:Invoke(MaxHealth)
		
	end)

Are you sure that every time the player respawn/resets a new connection is made?
You have to make sure every time the player’s character loads a new connection is made to the humanoid of the newly respawned character.

1 Like

Yes, that is infact happening.
Using knit to directly access the humanoid instead of localizing it once is how I achieve that.

Im not actually sure whats happening anymore haha

I’m sorry but I’m not convinced.
Could you add a print() on the line above where you connect the event?
It should print every time the player respawns.
If that’s the case and it still doesn’t work then i’m out of ideas.

I did add a print line, on both the .oninvoke and :Invoke().

Neither are fired.

I also used breakpoints to debug the signal itself, doesnt seem to work.

I also tried using the normal localized Humanoid variable instead of accessing it from Knit.

Also… Doesnt work.

Thats about as efficient as you can get. Now im just wondering what you could possibly doing to make it not efficient, if anything other things would be just micro-optimization. Whether it be removing the Bindable and having the items from the code run through there, or having the connection run in a different environment (Server or Client) depending on the work thats being done.

The only thing you can really do here is figure out where the issue is happening, whether that be when (or rather how) you create the signal, or when the Player is indexed as this isnt an inefficiency on the connection, rather, the code that is being ran within the bindable or module.

You should also be aware that the signal may not be aware of any changes made to the Health of the player

For example: You change the Health on the Client, but the connection is on the Server. The Server an only detect Server changes unless a Remote is used on the Client for the Server to do so.

Thats not going to do anything, its just reinventing the wheel.
Doing this just gives less features (variables), and focuses the event on a single property which I guess could be an optimization, but not by much.

Ive actually devised a pretty clever way to remove networking entirely from the process.

Since im using 2 scripts (Using Knit) to perform this desired action. Knit can handle the cross script communication.

By using :GetController() I can use Modulescript methods across different scripts!

Therefore, I created a method in the module that handles the text I desire to change, and instead firing a event every single time the humanoid regenerates or changes maxhealth. I am now simply calling a 1 line method from another modulescript.

function HealthBarController:SetTotalHealthText(MaxHealth)
	TotalHealthText.Text = MaxHealth
end
	Knit.Player.Character.Humanoid.HealthChanged:Connect(function()
		
		local Humanoid: Humanoid = Knit.Player.Character.Humanoid
		
		task.spawn(function()
			Knit.GetController("HealthBarController"):SetTotalHealthText(Humanoid.MaxHealth)
		end)

This isn’t going to change unless you actually change it yourself. Try just Health …

1 Like

Dont get ahead of yourself, as this is what I have stated:

That isnt exactly what you call a clever solution, merely a reduction of steps taken during the process.
Other than that, any extra form of optimization can be done by either reducing the amount of functions you use, and or having the code run directly through the module, of which this by itself can be considered a redundant function, and can otherwise be removed to save on calls:

You can remove the thread here as well, because the connection will already make one by itself:

That in turn will save some a very negligable amount of processing power

As stated before, these are very micro-optimizations that would otherwise make a very negligable difference, and if they are causing any issues with lag, its likely an issue on your end whether it be a memory leak, or the time it takes to calculate something.

Have a good rest of your day.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.