Humanoid.Died runs only once, because the humanoid becomes nil. This is a server sided script for my mini-game which teleports all users to their platform, I am checking if they died during round it teleports them back(so they don’t jump off or other bugs) but I can’t seem to find anyway to make it work after one death. Any ideas?
use CharacterAdded.
player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
--whatever is needed.
end)
end)
You can wrap a CharacterAdded event so when a new character is made, you can check the Humanoid.Died event on the new character.
local Players = game:GetService("Players")
local function PlayerAdded(player)
local function CharacterAdded(character)
local Humanoid = character:WaitForChild("Humanoid")
local function Died()
print(string.format("%s died!", player.Name)
end
Humaoid.Died:Connect(Died)
end
player.CharacterAdded:Connect(CharacterAdded)
end
Players.PlayerAdded:Connect(PlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
PlayerAdded(player)
end
This doesn’t seem to run at all. I added the playeradded like you did here
Since I was already looping through a table of playing players.
function PlayerAdded(player)
local function CharacterAdded(character)
local Humanoid = character:WaitForChild("Humanoid")
local function Died()
print("a")
player.CharacterAdded:Wait()
local findMap
for i, v in pairs(game.Workspace:FindFirstChild("MorseCodeMaps"):GetChildren()) do
if string.find(v.Name, player.Name) then
findMap = v
end
end
if findMap then
wait(0.5)
Humanoid.CFrame = findMap.SpawnLocation.CFrame
end
end
Humanoid.Died:Connect(Died)
end
player.CharacterAdded:Connect(CharacterAdded)
end
maybe the reason is you didnt connect the playeradded function to the playeradded connection in the players service
Just to give you a general idea of why Humanoid.Died only fires once:
- When a character dies their character (along with the Humanoid) is reloaded. This means once died first the Humanoid is destroyed.
You’ll need to either:
- Only use the current Humanoid of the player (only dies once though)
- Or connect to each player’s CharacterAdded, get each new character’s Humanoid, set up the Humanoid connections, then remember to disconnect all of the CharacterAdded events when the round ends/when you want the died event to disconnect
Edit:
Yep! Disconnecting the Died events works too, and so does having a check to see if it should run.
Or just disconnect all of the “.Died” events instead (in case the “.CharacterAdded” events are necessary when in and out of rounds). Alternatively, you could use a state variable/flag to determine if a function’s body should be executed or not.
I will be disconnecting them!