Detect player death around

local Players = game:GetService("Players");
local MaxMoralLoweringDistance = 30;

Players.PlayerAdded:Connect(function(player) -- Fires when a player joins the game
	player.CharacterAdded:Connect(function(character) -- Whenever the player character respawns
		local humanoid = character:WaitForChild("Humanoid");
		humanoid.Died:Connect(function() -- When the player dies
			CheckNearbyTeamMembers(player, character.HumanoidRootPart.Position, "BlueTeam"); -- Run the distance check
			print(character.Name.." Died");
		end)
	end)
end)

function CheckNearbyTeamMembers(diedplayer, position, team) -- 
	local players = Players:GetChildren();
	
	for i ,v in pairs(Players:GetChildren()) do
		if (v and v ~= diedplayer and v.Character ~= nil and v.Character:FindFirstChild("HumanoidRootPart")) then -- You can add the team check here
			local dist = (v.Character.HumanoidRootPart.Position - position).Magnitude;
			if (dist < MaxMoralLoweringDistance) then -- If the distance is lower than the max range
				-- Rest of your logic here
			end
		end
	end
end

Code should be placed in ServerScriptService.

I’m not sure how you game or team logic is set up. But this should be straight forward to understand.

When a player joins the game, you hook up the CharacterAdded event to them. Every time their character respawns it hooks up a Died event to the Humanoid associated to that spawned character.

You can simply run a distance check and pass in data of the died player (To not distance check against the dead player), position of the dead character and the team they’re on. The rest of the code is up to you to manage.

I can verify its working on my end, perhaps on your UI its not updating the Value if its displayed on a text box?

Can you test it again on your end?

I will just check if it is working - if it is I’ll edit this message

Alright - it seems to be working to a certain degree?

It’s not putting the value into the text

Are you using a listener to listen whenever the value changes? Ill look in roblox studio now, what is the script for the textbox and wheres the textbox located?

Edit:
I editted your listener, test that out now.

image

It’s probably because it doesn’t have a while true do - just noticed.

Player = game.Players.LocalPlayer
repeat wait() until Player.Character


Character = Player.Character
Humanoid = Character:WaitForChild("Humanoid")
MoraleValue = script.parent.MoraleNumber.Value

script.Parent.Morale.Text = MoraleValue

script.parent.MoraleNumber.Changed:Connect(function()
    script.Parent.Morale.Text = MoraleValue
end)```

I editted it and added a Changed to it, try that out, respond if its not working, if its working then you can mark this as solution
:smile:

Your solution didn’t seem to work so I’m going to try a while true do

nononono never use a while true script,

It causes way too much lag, hold on a sec while i try it again.

try that/

image
image

image

Number isn’t showing at all now,

Oops. fixed, i added value be accident

Still shows up as Text instead of an actual number.

That would be because i think you deleted the text changing part, it should be fixed.

if this works, mark this as solution.

I had to change it to a way I knew it would work - but it works now.

I changed it so that instead of MoraleValue being used it was the directory
script.parent.MoraleNumber.Value

So my initial script works??

If so you can mark this as solution if you want so if anyone else wants to know how to do this they have the solution right there.

1 Like