What could i do to improve my damage indication system?

Currently i have a damage indication system which has the client fire a remote event when the clients humanoid takes damage

Code:

local Player = game.Players.LocalPlayer

local DamageEvent = game.ReplicatedStorage.DamageIndicator

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local LastHealth = Humanoid.MaxHealth

Humanoid.HealthChanged:Connect(function(Health)
	if LastHealth > Health then
		
		local NegatedHealth = LastHealth - Health
		LastHealth = Health
	
		DamageEvent:FireServer(NegatedHealth)
		
	end
end)

and then on the server its pretty simple just a billboard gui. But i thought about it and what if i have many people taking damage at the same time this event will go off many times and it could be bad. Anyway to improve it?

1 Like

Have your server listen for when the health changes instead of the client. The way you currently have it set up is exploitable (i.e. they change their health locally really fast). Then send a remote to the clients from the server saying that this player took damage.

Have you tried respawning and seeing if it still works? If it doesn’t then your code is not programmed to handle when players respawn. You could try to throw the local script side into PlayerGui and set PlayerGui.ResetOnSpawn to true, but I humbly ask that you don’t do that lol.

1 Like

My first idea was to make it run on the server but i thought i would run into performance issues because i would have to do something like:

game.Players.PlayerAdded:Connect(function(Player)
	Player.Character:FindFirstChildOfClass("Humanoid").HealthChanged:Connect(function()
		
		--Damage Indicator Script Here
		
	end)
end)

Are there any downsides to this? And how would i track the humanoids last healtb?

  • My code would still work when i respawn but it would still count as damage taken. if i were to reset.

There are definitely downsides like offloading the performance to the server, but that performance dip is negligent (so, probably not even a real downside).

There’s a third route you can take, and it’s honestly the absolute BEST way to handle this situation. I didn’t explain it before because it might be a bit too complicated, but hear me out:

In summary, you’re going to do everything here on the client, but you’re also going to listen to OTHER players’ humanoids on your client as well:

  1. Player takes damage
  2. On that player’s client, we do the damage indicator script
  3. Another player loads into the game
  4. Upon them joining we listen to their humanoid as well, and run the damage indicator script on them when they take damage

Here’s a reference to get you started on listening for when other players’ characters get added btw, you might even be able to just paste your code under the CharacterAdded() function directly and it may work:

The reason why this is the best is because everything is done on your client. You have the added protection of only reacting to what the server authorizes, so no exploit potential. It also is “cleaner” code in a way, since it’s only reactive to what’s happening on your client.

1 Like

I didnt even think about this route. Its even better for tweens to be added since tweens on the servers are bad. Ill definitely try this.

1 Like

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