How do I use a server script for HP and play a sound locally?

I’m using my own custom HP system. I’m doing this in a serverscript so players will actually die and others will be able to see it, but I want to play a sound when a player is damaged that only that one user can hear?

For the health system, the values are managed on the server. However, the visuals, including healthbar and the client-only sound should be played on client only. Also:

  • Whenever a player deals damage, server should fire a remote for the attacker to play a sound, if I assume it’s a hitmarker.
  • Whenever a player takes damage, server should fire a remote for the target to play a sound.

i’ve never used a remote in my life not gonna lie to you

I don’t think you actually need to fire a remote event, just use an event to listen to health changes locally.

That’s true I could probably just do it in the hp ui?

You’d begin by putting a Remote Event in a place where both Client and Server can access it (ReplicatedStorage). You’d then make the server script (or whatever script is causing damage to the player) fire that Remote Event when you want the sound to play:

remoteEvent:FireClient(player)

In a LocalScript somewhere, you’d have to then script the part that plays the sound when the Remote Event is fired from the server:

remoteEvent.OnClientEvent:Connect(function()
 -- your code to play the sound
end)

And that’s pretty much all. Since it’s a Server to Client remote you wouldn’t even need to add any safety precautions for this (I doubt exploiters would want to annoy themselves by spamming a sound…).

Alternatively, if you really don’t want to bother with Remotes, you could just make a LocalScript with an event that listens for Humanoid.HealthChanged, which triggers the sound to play when the health becomes lower. I don’t know if this is a particularly efficient method though, but it should work and it will only play on the single player since it’s a LocalScript.

I’m not using roblox’s default health thing, i’m using my own that involves a numbervalue but something like this might work

The same principle really applies. I assume you probably store an IntValue for the health somewhere then? There’s also an event for that. It’s intValue.Changed, I believe.

Edit: My bad, misread it, just replace intValue with numberValue. The event for it would be numberValue.Changed.

Oh, that is actually possible but this depends a lot on whether the default humanoid was used.

It’s a startercharacter but it has a humanoid, i’m just not using the health thing