Detect player death around

Hi, I’m trying to make a GUI where if a teammate dies near then your “morale” will go down (this would be a TextLabel which would start off at 100 but if a teammate near you died it would go down by 3 then would slowly go back to 100)

I have no idea how you can detect player deaths around a person though and I don’t see anything on the devforum so was wondering if anybody knew how to do this?

2 Likes

So here are two useful events, functions that will help u to achieve your goal:

  1. There is a Humanoid.Died event, with this event u can detect the player`s death.
  2. For the distance from a player u can use :GetDistanceFromCharacter()

Good luck!

1 Like

These work in LocalScripts? (30 characters)

1 Like

Even if they don’t work in a LocalScript you can get the username of both users and therefore fire an Event to the client to do functions in a localscript.

1 Like

I’ve never used Humanoid.Died or GetDistanceFromCharacter so I’ll need to research that first before trying to add events and stuff.

1 Like

Also, I did talk about teammates and not just players in general? How would I make it so that the script would only detect deaths of teammates?

1 Like

You would check if they are on the same team colour/name as you, I will follow up to this message with an edit with a link on how to do so.

1 Like

I’m also still confused on how I’d mash the DistanceFromCharacter and Humanoid.Died together. Also it only shows one code sample which is of no use in DistanceFromCharacter and Humanoid.Died is set up so it detects any players death.

1 Like

You would listen for Humanoid.Died, when its fired, do an in pairs of every player and run DistanceFromCharacter to the user that died.

To check the players team is true it would be like this;

if player.Team == player2.Team then
--code
end

And for listening to the Death of a humanoid it would be like this;

local Players = game.Players:GetPlayers()

Players:WaitForChild("Humanoid").Died:Connect(function(plr)
		
for _, v in pairs(Players) do

    if v:DistanceFromCharacter(plr.Parent.HumanoidRootPart.Position) <= 10 and v.Team == game.Players[plr.Name].Team then
--fire consequences
end

end

		end)

Ran into another error where it won’t detect if a player has died if it’s in a player’s GUI. The important thing is that the GUI must detect if a player around them has died as the GUI changes a TextLabel.

I’m not sure but wouldn’t using an event affect everyone’s GUI if it was to change?

I just wrote some code above, please look and learn from it and tell me if you need anymore help

local Players = game.Players:GetPlayers()

Players:WaitForChild("Humanoid").Died:Connect(function(plr)
		
for _, v in pairs(Players) do

    if v:DistanceFromCharacter(plr.Parent.HumanoidRootPart.Position) <= 10 and v.Team == game.Players[plr.Name].Team then
--fire consequences
			
		end

end

		end)

I’m guessing this would be put in ServerScriptService as events are mentioned ,yes?

This doesn’t seem to work…

Here is my script in ServerScriptService
local Players = game.Players:GetPlayers()

Players:WaitForChild("Humanoid").Died:Connect(function(plr)
		
for _, v in pairs(Players) do

    if v:DistanceFromCharacter(plr.Parent.HumanoidRootPart.Position) <= 10 and v.Team == game.Players[plr.Name].Team then
--fire consequences
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    local remoteEvent = ReplicatedStorage:WaitForChild("Death")
     
    -- Fire the remote event
    remoteEvent:FireServer()
	
end
end
end)

Here is my script within the GUI itself

local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local remoteEvent = ReplicatedStorage:WaitForChild("Death")

local function OnClientEvent()
script.parent.MoraleNumber.Value = script.parent.MoraleNumber.Value - 3

remoteEvent.OnClientEvent:Connect()

The RemoteEvent is in ReplicatedStorage so it should be working so I’m kind of stumped here…
I also made sure that both players were on the same team.

image

Unless this is broken and it actually works fine in-game then I am confused.

Does it output any errors, and have you tried using print() as a diagnoses to see what part of it specifically isnt working?

Underneath is the stuff that is preventing it from firing.

image
image

I think I know what the issue is - you’re trying to find the player’s humanoid but the player doesn’t have a humanoid - the character is the one with the humanoid.

local Players = game.Players:GetPlayers()
local character = Players.Character
character:WaitForChild("Humanoid").Died:Connect(function(plr)
		
for _, v in pairs(Players) do

    if v:DistanceFromCharacter(plr.Parent.HumanoidRootPart.Position) <= 10 and v.Team == game.Players[plr.Name].Team then
			--fire consequences
			print ("eeeee")
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    local remoteEvent = ReplicatedStorage:WaitForChild("Death")
     
    -- Fire the remote event
			    remoteEvent:FireServer()
	
end
end
end)

Changing this however brought about a new error
image

OH MY BAD!

Nono I forgot, change to this:

Players.Character:WaitForChildren

^
See if this works.

Players.Character:WaitForChildren(“Humanoid”).Died:Connect(function(plr)

???

Yes try that. that may work. Correct me if incorrect.

image

As you can see it still produces an error - I’m certain it has something to do with the Humanoid but I don’t normally do this type of scripting.

Hmm looks like what I tried to use as reference to create it was incorrect.

Change it to: FindFirstChild() if not please respond.