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?
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.
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.
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?
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?
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.
Unless this is broken and it actually works fine in-game then I am confused.
Underneath is the stuff that is preventing it from firing.
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)