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.
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.
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.
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)
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.