Detect player death around

s is the character, not the player

Just edited, copy and paste that.

Will test this out on my own client side to get solution correct.

What did you change and why? (30 characters)

image
Also the error is still appearing - I also thought myself that it was capitalization but apparently not

the variable “character” isnt needed. you could just use

s.Humanoid.Died:Connect(function(plr)

or change s to charcater

i see the problem here. you have to check for PlayerAdded before characteradded, and also .died doesnt have a parameter to get the specific player.

game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(s)
s.Humanoid.Died:Connect(function(plr)
for _, v in pairs(game.Players:GetPlayers()) do
if v.Name ~= p.Name then
    if v:DistanceFromCharacter(s.HumanoidRootPart.Position) <= 10 and v.Team == game.Players[p.Name].Team then
			--fire consequences
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    local remoteEvent = ReplicatedStorage:WaitForChild("Death")
     
    -- Fire the remote event
			    remoteEvent:FireClient(v)
	end
end
end
end)
	end)
	end)

Tested and fixed, now you just need to code the client side for the consequences,
If you find any more issues, it should be pretty straight forwards to fix but ive tested it.

image

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("Death")

local function OnClientEvent()

script.parent.MoraleNumber.Value = script.parent.MoraleNumber.Value - 3

end

remoteEvent.OnClientEvent:Connect()

It references the final line, should this not be correct? This is the LocalScript inside the GUI

EDIT: think i’ve found the error

Oh my… Ill create an edit for this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("Death")


remoteEvent.OnClientEvent:Connect(function(plr)
script.parent.MoraleNumber.Value = script.parent.MoraleNumber.Value - 3
end)

Do I have to reference plr? (30 characters)

game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(s)
s.Humanoid.Died:Connect(function(plr)
for _, v in pairs(game.Players:GetPlayers()) do
if v.Name ~= p.Name then
    if v:DistanceFromCharacter(s.HumanoidRootPart.Position) <= 10 and v.Team == game.Players[p.Name].Team then
			--fire consequences
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    local remoteEvent = ReplicatedStorage:WaitForChild("Death")
     
    -- Fire the remote event
			    remoteEvent:FireClient(v)
	end
end
end
end)
	end)
	end)

Cause as you can see (v) is referenced here, so do I change it?

No, Its just there as a variable

I don’t think it’s working (local script), even though I’ve tested this code before (without events) and it should work.

try removing plr since i dont think its the issue, run it and show me console.

There aren’t any errors in output, it just doesn’t seem to work.

Removing the plr thing may have fixed it so I’ll do the local server thing.

Update: It is not working and no errors are displaying in output - the morale value does not change either

did you use a print to see that it did in fact fire the client? please check to make sure it is indeed firing.

if it does fire, we can infact see that the error lies in the process changing the value.

I also want to make an edit to the initial server script to make it all run off server, so its secure.

In the server or the local script?

This will skip past firing events and do it all on the server side so it displays to everyone, try this.

So try this on the server side, besure to change MoraleNumber location as im unsure where you have placed it.

game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(s)
s.Humanoid.Died:Connect(function(plr)
for _, v in pairs(game.Players:GetPlayers()) do
if v.Name ~= p.Name then
    if v:DistanceFromCharacter(s.HumanoidRootPart.Position) <= 10 and v.Team == game.Players[p.Name].Team then
			--fire consequences
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
     
    local remoteEvent = ReplicatedStorage:WaitForChild("Death")
     
			    v.PlayerGui.GUI.MoraleNumber.Value =   v.PlayerGui.GUI.MoraleNumber.Value - 3
	end
end
end
end)
	end)
	end)

ServerScriptService is where this would be, yes?

Do you want me to try fix this by collaborating with you in roblox studio instead so I can diagnose it with knowing where the MoraleNumber is and all that?

If so you can add me to team create: WolfieTheDino.

Sure - you should see some personal messages now.

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.