Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

nil to Death?

Asked by 10 years ago

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

Log in to vote
0
Answered by 10 years ago

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!

Ad
Log in to vote
0
Answered by
SanityMan 239 Moderation Voter
10 years ago

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!

Answer this question