Attempt to index nil with "FindFirstChild"

Trying to make a custom admin command for removing tools. The part that’s not working is when trying to remove a tool from everyone.
image_2023-10-30_170438795

Commands.removetool = function(admin,player,tool)
	if player then
		if player == "all" then
			player = game.Players:GetChildren() 
			tool = player.Backpack:FindFirstChild(tool) -- this is where the error is
			
			print(admin.Name.." has removed "..tool.Name.." from "..player.Name)

			if player and tool then
				tool:Destory()
			end
		else do
				player = game.Players:FindFirstChild(player)
				tool = player.Backpack:FindFirstChild(tool)

				print(admin.Name.." has removed "..tool.Name.." from "..player.Name)

				if player and tool then
					tool:Destory()
				end
			end
		end
	end
end

return Commands

instead of

			player = game.Players:GetChildren()
			tool = player.Backpack:FindFirstChild(tool)
			
			print(admin.Name.." has removed "..tool.Name.." from "..player.Name)

			if player and tool then
				tool:Destory()
			end

do

local players = game.Players:GetChildren()
for _, player in players do
    local tool = player.Backpack:FindFirstChild(tool)
    if tool then
        tool:Destroy()
    end
end

print(admin.Name.." has removed "..tool.." from "..player.Name)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.