Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

nil to Death?

Is their anyway to make this more efficient? This is located inside the workspace. Sometimes it will say that the humanoid is nil. I put if ~= nil, but that don't work.

while true do wait()
local Players = Game.Players:GetChildren() 
for i = 1, #Players do
if Players[i].Character.Humanoid.Health == 0 then
end
end
end

2 answers

Try this;

while wait(0) do
local Players = Game.Players:GetChildren()
for i = 1, #Players do
if Players[i].Character:FindFirstChild("Humanoid")then --If Script finds Humanoid
if Players[i].Character.Humanoid.Health == 0 then
end end end end

I hope this helped!

I believe that this way (using a function triggered by the death) is more efficient than a while or for loop.

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
--Then I believe you would do what you want here
end)
end)
end)

Hope it helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.