Attempt to index nil with ‘FindFirstChild’

So I am making a script, but it had an error.
Script:

local teams = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
	if team.Name == "In-Game" and #players <= 1 then
		local humanoid = players.Character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = 0
			break
		end
	end
end

EDIT: Sorry, bad script

I assume their Character hasn’t been loaded yet.

1 Like

I’m not quite sure about what team:GetPlayers().Character returns, but I think it’s a table, so not an object. Let me check first though, before I explain something that isn’t true.

2 Likes

The table will only one value, so how to table value to object?

I think

if team:GetPlayers().Character[1] then
    local humanoid = players.Character[1]:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
        break
    end
end
1 Like

:GetPlayers returns a table of current players. If you want to reference the actual players you can just use a for loop and reference the character using Player.Character.

for Index, Player in ipairs(team:GetPlayers)do
      local Character = Player.Character;
   -- do whatever else
end;

Note I’m on mobile so there may be typos

1 Like

So I just found out team:GetPlayers().Character returns nothing. You would have to use a for loop.

for _, player in ipairs(team:GetPlayers()) do --Go through every value in the table, ipairs is faster because it isn't a random order.--
    local character = player.Character or player.CharacterAdded:Wait() --Get the character or wait for the character if it isn't there yet.--
    local humanoid = character:FindFirstChildWhichIsA("Humanoid") --Check if there is a humanoid inside the character / reference the humanoid inside the character.--
    if humanoid then --Check if there is a humanoid inside the character.--
        humanoid.Health = 0 --Set the humanoid's health property to 0 so the character dies.--
        break --Break the loop.--
    end
end

Edit after 6 minutes:
I totally forgot you checked if it was only one person before, therefore your way is also great.

1 Like

I find a solution,

local teams = game:GetService("Teams"):GetTeams()
for _, team in pairs(teams) do
	local players = team:GetPlayers()
	if team.Name == "In-Game" and #players <= 1 then
		local Character = players[1]
		local humanoid = Character.Character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = 0
			break
		end
	end
end

Thank you @iamajust @Ze_tsu @milanomaster

That will only take the FIRST player in the table and kill him. Every other player will be unaffected. You can test this by going in studio and doing a 2 player play test.

A for loop may be more organized, but:

So in this case, it doesn’t really matter.

I know, so I wrote the script is least TWO players.

What do you mean? The script checks if there is one player or less.
DevHub API

1 Like

I have a question:
If the last player of the In-game team left. It will index nil?

Yup. Therefore check it like I did in one of my previous posts. (Only take that part, not the whole script.)

Edit after 2 minutes:
Make it:

if Character then

1 Like

This will still throw an error, potentially. You check if the size of players is less than or equal to 1. When it’s 0, this script will error as players[1] will be nil.

Assuming you want to kill all players in a single team, here’s what you’d do:

local team = game:GetService("Teams").InGame

local players = team:GetPlayers()
for _, player in ipairs(players) do
	local character = player.Character
	local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

For just the first player:

local team = game:GetService("Teams").InGame

local player = team:GetPlayers()[1]
if player then
	local character = player.Character
	local humanoid = character and character:FindFirstChildWhichIsA("Humanoid") -- this checks `character` also exists
	if humanoid then
		humanoid.Health = 0
	end
end
1 Like