Thou Goal:
Get when any player from a table that contains all the players currently in the server dies. I need it to also know which character died, or atleast the name of the player, and have it print out the name of the player that died.
Thy Issue:
I’m gonna be real with y’all, got absolutely no idea how to do this. Tables are NOT my thing.
Thy(ran out of alternatives to thyAttempts To Solve Thou Issue:
I’ve attempted to look through a couple posts but I couldn’t find much for this problem.
oh yeah heres the code:
local players = game:GetService("Players")
local gui = script.Parent.Parent.Parent.Parent
local playersTable = {}
local function addPlayer(player)
table.insert(playersTable, player)
for _, playerName in ipairs(playersTable) do -- this was here for testing shenanigans
print(playerName)
end
end
local function removePlayer(player)
table.remove(playersTable, table.find(playersTable, player))
print(player.Name) -- here too
end
players.PlayerAdded:Connect(addPlayer)
players.PlayerRemoving:Connect(removePlayer)
-- INITIATOR
for _, player in ipairs(game.Players:GetPlayers()) do
table.insert(playersTable, player.Name)
print(player.Name)
end
Oh! One more thing, I’ll likely be gone for three and a half hours. I’ll read any and all replies once I get back, thank you!
You could just use the humanoid.Died event to do this. For your code it’ll look something like this:
local players = game:GetService("Players")
local gui = script.Parent.Parent.Parent.Parent
local playersTable = {}
local function addPlayer(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
print(player.Name, character) --print player's name and character model that died
end)
end)
end
local function removePlayer(player)
table.remove(playersTable, table.find(playersTable, player))
print(player.Name) -- here too
end
players.PlayerAdded:Connect(addPlayer)
players.PlayerRemoving:Connect(removePlayer)
I’m not sure if you still need a table of players in the game? You can just do
There is a documentation for this on the creator hub. here is the code from the documentation:
local Players = game:GetService("Players")
local function onPlayerAdded(player)
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local function onDied()
print(player.Name, "has died!")
end
humanoid.Died:Connect(onDied)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
simpler
humanoid.Died:Connect(function(died)
--idk If died is right in the print. you can test around what died prints. if this doesn't work just use the longer one.
print(died)
end)