Attempt to index nil with ‘Humanoid’ can’t be solved
I’ve tried many times and need your help because it doesn’t work
running = true
function start()
local players = game.Players:getChildren()
local connections = {}
running = true
for i=1,#players do
local Humanoid = players[i].Character:WaitForChild("Humanoid")
if players[i].Character ~= nil then
setTag(players[i], "BoolValue", "survived", true)
table.insert(connections, players[i].Character.Humanoid.Changed:connect(function() died(players[i],players[i].Character.Humanoid)end))
end
end
while running do
wait(1)
end
players = game.Players:getChildren()
local survivedt = {}
for i=1,#players do
local survived = getTag(players[i], "BoolValue", "survived", false)
if survived then
setTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Survivals", getTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Survivals", 0) + 1)
setTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Points", getTag(players[i]:findFirstChild("leaderstats"), "IntValue", "Points", 0) + bin.points.Value)
table.insert(survivedt, players[i])
end
end
bin.points.Value = 0
for i=1,#connections do -- Disconnect all previous connections.
connections[i]:disconnect()
end
if #survivedt > 0 then
feedback(game.Workspace, compilePlayers(survivedt).. " survived.")
else
feedback(game.Workspace, "Nobody Survived!")
end
removeFeedback(game.Workspace, 3)
It looks like you are trying to insert a connection into a table, that handles when they die and calls a function? The character is nil, so that means either you have to wait for it to be added or you aren’t getting a player from the table. Are you sure that players[i] is actually indexing a player? If we can see more of your code that would be helpful.
You should use a for loop, Why? It’ll give you the index & value. You won’t need to get the players value from the index. Oh and for the “connect” event function It’s depreciated, use “Connect” instead.
Also do not use GetChildren on Players Service, there’s a better function called “GetPlayers”.
Try this, see if it works:
for _,k in pairs(game:GetService("Players"):GetPlayers()) do
if k.Character and k.Character:WaitForChild("Humanoid") then
local Hum = k.Character.Humanoid
setTag(k, "BoolValue", "survived", true)
table.insert(connections, Hum.Changed:Connect(function()
died(k, Hum)
end))
end
end
I recommend you to use pairs, since they are more readable. For example
for _, player in pairs(players) do
print(player.Name)
end
…is much easier to read than
for i=1,#players do
print(players[i].Name)
end
Also, why are you checking if ‘players[i].Character ~= nil’ after you already use 'players[i].Character. And you are not using your ‘Humanoid’ variable.
for i=1,#players do
local Humanoid = players[i].Character:WaitForChild("Humanoid")
if players[i].Character ~= nil then
setTag(players[i], "BoolValue", "survived", true)
table.insert(connections, players[i].Character.Humanoid.Changed:connect(function() died(players[i],players[i].Character.Humanoid)end))
end
end
To
for _,player in pairs(players) do
local char = player.Character
local hum = char and char:WaitForChild("Humanoid")
setTag(player, "BoolValue", "survived", true)
table.insert(connections,hum.Changed:Connect(function()
died(player,hum)
end))
end