Attempt to index nil with findfirstchild

So my code is giving me: attempt to index nil with findfirstchild on line 75:

	for i, player in pairs(players) do
		if players[i] and player.Character and players.Character:FindFirstChild("Stuff") then
			task.spawn(function()
				fakelist[player] = {
					player.Name
				}
			end)
		end
	end

Full Code:
local TS = game:GetService(“TeleportService”)

–local maps = {
– [1] = {
– [“id”] = 8159140940;
– [“name”] = “Forest”
– };
– [2] = {
– [“id”] = 8176142524;
– [“name”] = “Arcade”
– };
–}

local maps = {
[1] = {
id = 8159140940,
name = “Forest”
};
[2] = {
id = 8176142524,
name = “Arcade”
};
}

local players = {}
local maxBots = 1
local botsjoined = 0
–local playersOk = Instance.new(“BindableEvent”)

local joiner = workspace.ClientJoin.OnServerEvent:connect(function(p)
print(p.Name…" joined")
table.insert(players, p)
workspace.PlayersInWait.Value = #players + botsjoined
end)

while task.wait(3) do
wait()
repeat wait() until #players > 0

--wait(math.random(1,2))
--workspace.PlayersInWait.Value = workspace.PlayersInWait.Value + 1
--local botsjoined = 1

--spawn(function()
--	wait(math.random(1,2))
--	workspace.PlayersInWait.Value = workspace.PlayersInWait.Value + 1
--	local botsjoined = 2
--	wait(math.random(1,2))
--	workspace.PlayersInWait.Value = workspace.PlayersInWait.Value + 1
--	local botsjoined = 3
--end)
--wait()

for i = 10, 0, -1 do
	workspace.TimeTilStart.Value = i

	print(i)

	if #players < 5 then
		task.wait(1)
	end
end

joiner:Disconnect()

if #players < 1 then continue end

local pickedmap = maps[math.random(1, #maps)].id
print(pickedmap)

local code = TS:ReserveServer(pickedmap)
local fakelist = {}

for i, player in pairs(players) do
	if players[i] and player.Character and players.Character:FindFirstChild("Stuff") then
		task.spawn(function()
			fakelist[player] = {
				player.Name
			}
		end)
	end
end

task.wait(1)
TS:TeleportToPrivateServer(pickedmap, code, game.Players:GetPlayers(), nil, script.LoadScreen)
workspace.PlayersInWait.Value = 0

end

1 Like

What line is line 75? Also, attempt to index nil with … usually means that the thing before it / the thing your indexing is nil, as in non existent or not loaded, so check if spelling is correct and the thing is referred to correctly.

1 Like

Players need to be Player before the FindFirstChild().

This:

players.Character:FindFirstChild("Stuff")

Needs to be this:

player.Character:FindFirstChild("Stuff")

Hope this helped :slight_smile:

That means “Stuff” returned nil.

1 Like

What about the misspelling of this?

I think it means the parent of what your tring to find.

	for Index, v in ipairs(game.Players:GetPlayers()) do
        local player = game.Players[i]

		if player and player.Character and player.Character:FindFirstChild("Stuff") then
			task.spawn(function()
				fakelist[player] = {
					player.Name
				}
			end)
		end
	end

That should work, I fixed it up a bit.

1 Like

That would be another issue, however, the current code was messy. First off, ipairs is way faster and more reliable. And second, we are looking for the Index, not the variable.

1 Like

There is an unnecessary definition of player here. The v variable stands for the player, so there is no need to make another variable specifically for it.

for i, v in ipairs(game.Players:GetPlayers()) do
	if v and v.Character and v.Character:FindFirstChild("Stuff") then
		task.spawn(function()
			fakelist[v] = {
				v.Name
			}
		end)
	end
end

Also i wud point out that if u r storing data then its better to store the player’s UserId instead of name.

1 Like

This will maybe work for you